java 过滤器模式(Filter/Criteria Pattern)详细介绍
程序员文章站
2024-03-12 14:32:14
java 过滤器模式(filter/criteria pattern)
过滤器模式(filter pattern)或标准模式(criteria pattern)是一种设计...
java 过滤器模式(filter/criteria pattern)
过滤器模式(filter pattern)或标准模式(criteria pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。
过滤器模式(filter pattern)或标准模式(criteria pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。
实现
我们将创建一个 person 对象、criteria 接口和实现了该接口的实体类,来过滤 person 对象的列表。criteriapatterndemo,我们的演示类使用 criteria 对象,基于各种标准和它们的结合来过滤 person 对象的列表。
步骤 1
创建一个类,在该类上应用标准。
person.java
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 string getgender() { return gender; } public string getmaritalstatus() { return maritalstatus; } }
步骤 2
为标准(criteria)创建一个接口。
criteria.java
import java.util.list; public interface criteria { public list<person> meetcriteria(list<person> persons); }
步骤 3
创建实现了 criteria 接口的实体类。
criteriamale.java
import java.util.arraylist; import java.util.list; public class criteriamale implements criteria { @override public list<person> meetcriteria(list<person> persons) { list<person> malepersons = new arraylist<person>(); for (person person : persons) { if(person.getgender().equalsignorecase("male")){ malepersons.add(person); } } return malepersons; } }
criteriafemale.java
import java.util.arraylist; import java.util.list; public class criteriafemale implements criteria { @override public list<person> meetcriteria(list<person> persons) { list<person> femalepersons = new arraylist<person>(); for (person person : persons) { if(person.getgender().equalsignorecase("female")){ femalepersons.add(person); } } return femalepersons; } }
criteriasingle.java
import java.util.arraylist; import java.util.list; public class criteriasingle implements criteria { @override public list<person> meetcriteria(list<person> persons) { list<person> singlepersons = new arraylist<person>(); for (person person : persons) { if(person.getmaritalstatus().equalsignorecase("single")){ singlepersons.add(person); } } return singlepersons; } }
andcriteria.java
import java.util.list; 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> persons) { list<person> firstcriteriapersons = criteria.meetcriteria(persons); return othercriteria.meetcriteria(firstcriteriapersons); } }
orcriteria.java
import java.util.list; 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> persons) { list<person> firstcriteriaitems = criteria.meetcriteria(persons); list<person> othercriteriaitems = othercriteria.meetcriteria(persons); for (person person : othercriteriaitems) { if(!firstcriteriaitems.contains(person)){ firstcriteriaitems.add(person); } } return firstcriteriaitems; } }
步骤4
使用不同的标准(criteria)和它们的结合来过滤 person 对象的列表。
criteriapatterndemo.java
public class criteriapatterndemo { public static void main(string[] args) { list<person> persons = new arraylist<person>(); persons.add(new person("robert","male", "single")); persons.add(new person("john","male", "married")); persons.add(new person("laura","female", "married")); persons.add(new person("diana","female", "single")); persons.add(new person("mike","male", "single")); persons.add(new person("bobby","male", "single")); criteria male = new criteriamale(); criteria female = new criteriafemale(); criteria single = new criteriasingle(); criteria singlemale = new andcriteria(single, male); criteria singleorfemale = new orcriteria(single, female); system.out.println("males: "); printpersons(male.meetcriteria(persons)); system.out.println("\nfemales: "); printpersons(female.meetcriteria(persons)); system.out.println("\nsingle males: "); printpersons(singlemale.meetcriteria(persons)); system.out.println("\nsingle or females: "); printpersons(singleorfemale.meetcriteria(persons)); } 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() +" ]"); } } }
步骤 5
验证输出。
males: person : [ name : robert, gender : male, marital status : single ] person : [ name : john, gender : male, marital status : married ] person : [ name : mike, gender : male, marital status : single ] person : [ name : bobby, gender : male, marital status : single ] females: person : [ name : laura, gender : female, marital status : married ] person : [ name : diana, gender : female, marital status : single ] single males: person : [ name : robert, gender : male, marital status : single ] person : [ name : mike, gender : male, marital status : single ] person : [ name : bobby, gender : male, marital status : single ] single or females: person : [ name : robert, gender : male, marital status : single ] person : [ name : diana, gender : female, marital status : single ] person : [ name : mike, gender : male, marital status : single ] person : [ name : bobby, gender : male, marital status : single ] person : [ name : laura, gender : female, marital status : married ]
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!