Today we bring you the filter mode, this mode is easy to understand, we cut to the chase!

The filter pattern is a structural pattern that focuses more on combinations of classes and objects. What is the filter mode? For example, if you have an animal class that has cats, dogs, lions, monkeys, etc., how do you filter out the dog objects from the animal class? How do you filter out objects that are both male and lion?

We call a filter condition a criterion, filter out the list of objects you want based on various criteria, and this sequence of actions is decoupled through code, which we call filter pattern.

Now for example, there is a computer class, the computer is made up of many parts, such as CPU, memory and so on, we now filter the qualified computers according to the standards we want.

Class diagram:

Specific code implementation:

public class Computer {
    / / CPU model
    private String cpuType;
    // Memory size
    private int memorySize;
    // Operating system type
    private String operatingSystemType;

    public Computer(String cpuType, int memorySize, String operatingSystemType) {
        this.cpuType = cpuType;
        this.memorySize = memorySize;
        this.operatingSystemType = operatingSystemType;
    }

    public String getCpuType(a) {
        return cpuType;
    }

    public void setCpuType(String cpuType) {
        this.cpuType = cpuType;
    }

    public int getMemorySize(a) {
        return memorySize;
    }

    public void setMemorySize(int memorySize) {
        this.memorySize = memorySize;
    }

    public String getOperatingSystemType(a) {
        return operatingSystemType;
    }

    public void setOperatingSystemType(String operatingSystemType) {
        this.operatingSystemType = operatingSystemType;
    }

    @Override
    public String toString(a) {
        return "Computer{" +
                "cpuType='" + cpuType + '\' ' +
                ", memorySize=" + memorySize +
                ", operatingSystemType='" + operatingSystemType + '\' ' +
                '} '; }}public interface Criteria {
    public List<Computer> meetCriteria(List<Computer> computerList);
}

public class CriteriaI5 implements Criteria {

    @Override
    public List<Computer> meetCriteria(List<Computer> computerList) {
        List<Computer> i5Computer = new ArrayList<>();
        for(Computer computer : computerList) {
            if(computer.getCpuType().equals("I5")) { i5Computer.add(computer); }}returni5Computer; }}public class Criteria8G implements Criteria {
    @Override
    public List<Computer> meetCriteria(List<Computer> computerList) {
        List<Computer> _8gComputer = new ArrayList<>();
        for(Computer computer : computerList) {
            if(computer.getMemorySize() == 8 * 1024) { _8gComputer.add(computer); }}return_8gComputer; }}public class AndCriteria implements Criteria {

    private Criteria criteria;
    private Criteria otherCriteria;

    public AndCriteria(Criteria criteria, Criteria otherCriteria) {
        this.criteria = criteria;
        this.otherCriteria = otherCriteria;
    }

    @Override
    public List<Computer> meetCriteria(List<Computer> computerList) {
        List<Computer> computers = criteria.meetCriteria(computerList);
        returnotherCriteria.meetCriteria(computers); }}public class OrCriteria implements Criteria {

    private Criteria criteria;
    private Criteria otherCriteria;

    public OrCriteria(Criteria criteria, Criteria otherCriteria) {
        this.criteria = criteria;
        this.otherCriteria = otherCriteria;
    }

    @Override
    public List<Computer> meetCriteria(List<Computer> computerList) {
        List<Computer> computers1 = criteria.meetCriteria(computerList);
        List<Computer> computers2 = otherCriteria.meetCriteria(computerList);

        for(Computer computer : computers2) {
            if(!computers1.contains(computer)) {
                computers1.add(computer);
            }
        }
        returncomputers1; }}public class Client {
    public static void main(String[] args) {
        List<Computer> computerList = new ArrayList<>();

        computerList.add(new Computer("I5".1024."Windows"));
        computerList.add(new Computer("I5".1024 * 8."Linux"));
        computerList.add(new Computer("I7".1024 * 8."Windows"));
        computerList.add(new Computer("I7".1024."Linux"));
        computerList.add(new Computer("I3".1024 * 4."Linux"));


        Criteria criteriaI5 = new CriteriaI5();
        Criteria criteria8G = new Criteria8G();
        Criteria criteriaI5Or8G = new OrCriteria(criteriaI5, criteria8G);
        Criteria criteriaI5And8G = new AndCriteria(criteriaI5, criteria8G);

        System.out.println("cpuType = I5----------------------------");
        printComputer(criteriaI5.meetCriteria(computerList));
        System.out.println("memorySize = 8G-------------------------");
        printComputer(criteria8G.meetCriteria(computerList));
        System.out.println("cpuType = I5 or memorySize = 8G---------");
        printComputer(criteriaI5Or8G.meetCriteria(computerList));
        System.out.println("cpuType = I5 and memorySize = 8G--------");
        printComputer(criteriaI5And8G.meetCriteria(computerList));
    }

    public static void printComputer(List<Computer> computers) {
        for(Computer computer : computers) { System.out.println(computer); }}}/*
cpuType = I5----------------------------
Computer{cpuType='I5', memorySize=1024, operatingSystemType='Windows'}
Computer{cpuType='I5', memorySize=8192, operatingSystemType='Linux'}
memorySize = 8G-------------------------
Computer{cpuType='I5', memorySize=8192, operatingSystemType='Linux'}
Computer{cpuType='I7', memorySize=8192, operatingSystemType='Windows'}
cpuType = I5 or memorySize = 8G---------
Computer{cpuType='I5', memorySize=1024, operatingSystemType='Windows'}
Computer{cpuType='I5', memorySize=8192, operatingSystemType='Linux'}
Computer{cpuType='I7', memorySize=8192, operatingSystemType='Windows'}
cpuType = I5 and memorySize = 8G--------
Computer{cpuType='I5', memorySize=8192, operatingSystemType='Linux'}
*/
Copy the code

Well, this issue is so easy, I believe you will read it!

May everyone read this article with skepticism and explore how it works.

Road obstruction and long, the past for preface, the future for chapter.

Looking forward to our next meeting!