码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式-过滤模式

时间:2019-08-19 23:15:45      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:filter   inter   div   display   play   turn   each   ORC   private   

过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。

下面创建了CriteriaMale、CriteriaFemale、CriteriaSingle、AndCriteria、OrCriteria都实现了Criteria接口并重写了meetCriteria方法,在Client中可以根据需要过滤出所需要的数据

1.创建Criteria接口和Person实体类

技术图片
package com.design.demo.filter;

/**
 * @author: GuanBin
 * @date: Created in 下午11:11 2019/8/18
 */
public class Person {

    private String name;
    private String gender;
    private String maritalStatus;

    public Person(String name, String gender, String maritalStatus) {
        this.name = name;
        this.gender = gender;
        this.maritalStatus = maritalStatus;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getMaritalStatus() {
        return maritalStatus;
    }

    public void setMaritalStatus(String maritalStatus) {
        this.maritalStatus = maritalStatus;
    }
}
View Code
package com.design.demo.filter;

import java.util.List;

/**
 * @author: GuanBin
 * @date: Created in 下午11:14 2019/8/18
 */
public interface Criteria {

    public List<Person> meetCriteria(List<Person> personList);
}

 

2.创建CriteriaMale

package com.design.demo.filter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: GuanBin
 * @date: Created in 下午11:15 2019/8/18
 */
public class CriteriaMale implements Criteria {

    @Override
    public List<Person> meetCriteria(List<Person> personList) {
        ArrayList<Person> malePersons = new ArrayList<>();
        for (Person person : personList) {
            if (person.getGender().equalsIgnoreCase("MALE")) {
                malePersons.add(person);
            }
        }
        return malePersons;
    }
}

 

3.创建CriteriaFemale

package com.design.demo.filter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: GuanBin
 * @date: Created in 下午11:15 2019/8/18
 */
public class CriteriaFemale implements Criteria {

    @Override
    public List<Person> meetCriteria(List<Person> personList) {
        ArrayList<Person> femalePersons = new ArrayList<>();
        for (Person person : personList) {
            if (person.getGender().equalsIgnoreCase("FEMALE")) {
                femalePersons.add(person);
            }
        }
        return femalePersons;
    }
}

 

4.创建CriteriaSingle

package com.design.demo.filter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: GuanBin
 * @date: Created in 下午11:15 2019/8/18
 */
public class CriteriaSingle implements Criteria {

    @Override
    public List<Person> meetCriteria(List<Person> personList) {
        ArrayList<Person> singlePersons = new ArrayList<>();
        for (Person person : personList) {
            if (person.getMaritalStatus().equalsIgnoreCase("SINGLE")) {
                singlePersons.add(person);
            }
        }
        return singlePersons;
    }
}

 

5.创建AndCriteria

package com.design.demo.filter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: GuanBin
 * @date: Created in 下午11:15 2019/8/18
 */
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<Person> meetCriteria(List<Person> personList) {
        List<Person> people = criteria.meetCriteria(personList);
        return otherCriteria.meetCriteria(people);
    }
}

 

6.创建OrCriteria

package com.design.demo.filter;

import java.util.List;

/**
 * @author: GuanBin
 * @date: Created in 下午11:15 2019/8/18
 */
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<Person> meetCriteria(List<Person> personList) {
        List<Person> people = criteria.meetCriteria(personList);
        List<Person> otherPeople = otherCriteria.meetCriteria(people);

        otherPeople.stream().forEach(person -> {
            if(!people.contains(person)){
                people.add(person);
            }
        });

        return people;

    }
}

 

7.创建Client类

package com.design.demo.filter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: GuanBin
 * @date: Created in 下午11:27 2019/8/18
 */
public class Client {

    public static void main(String[] args) {
        List<Person> people = new ArrayList<>();

        people.add(new Person("lisi","Male","Single"));
        people.add(new Person("zhangsan","Male","Single"));
        people.add(new Person("wangwu","Female","Married"));
        people.add(new Person("zhangliu","Male","Married"));
        people.add(new Person("wangsi","Female","Single"));

        CriteriaMale criteriaMale = new CriteriaMale();
        CriteriaFemale criteriaFemale = new CriteriaFemale();
        CriteriaSingle criteriaSingle = new CriteriaSingle();

        //即为女士又是单身的人
        AndCriteria andCriteria = new AndCriteria(criteriaMale,criteriaSingle);
        //男士和单身的人
        OrCriteria orCriteria = new OrCriteria(criteriaFemale,criteriaSingle);

        System.out.println("-----Male");
        printPersons(criteriaMale.meetCriteria(people));
        System.out.println("-----Female");
        printPersons(criteriaFemale.meetCriteria(people));
        System.out.println("-----Single");
        printPersons(criteriaSingle.meetCriteria(people));
        System.out.println("-----Male and Single");
        printPersons(andCriteria.meetCriteria(people));
        System.out.println("-----Femal or Single");
        printPersons(orCriteria.meetCriteria(people));
    }

    public static void printPersons(List<Person> persons){
        for (Person person : persons) {
            System.out.println("Person : [ Name : " + person.getName()
                    +", Gender : " + person.getGender()
                    +", Marital Status : " + person.getMaritalStatus()
                    +" ]");
        }
    }
}

设计模式-过滤模式

标签:filter   inter   div   display   play   turn   each   ORC   private   

原文地址:https://www.cnblogs.com/guanbin-529/p/11380147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!