前言
2022/9/23 14:06
路漫漫其修远兮,吾将上下而求索
本文是根据袁庭新老师学习所做笔记
仅供学习交流使用,转载注明出处
推荐
袁庭新老师-视频:【Java设计模式】23种Java设计模式,全网最全精讲版本,熬夜爆肝也要学完【10:06:48】
狂神老师-笔记:设计模式【java提高】
过滤器模式
过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单―标准。
缺点:代码过于冗余
实现
我们将创建一个Person对象、Criteria接口和实现了该接口的实体类,来过滤Person对象的列表。CriteriaPatternDemo类使用Criteria对象,基于各种标准和它们的结合来过滤Person对象的列表。
1.创建一个项目名称为criteria_pattern的Java项目。
2.创建—个Person类,在该类上应用标准。
package com.js; public class Person { private String name; private String gender; //婚姻状态:single单身、marital表示已婚 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; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", gender='" + gender + '\'' + ", maritalStatus='" + maritalStatus + '\'' + '}'; } }
3.创建过滤规则接口
package com.js; import java.util.List; /** * 过滤器的接口(标准接口) */ public interface Criteria { /** * 实现的符号指定的过滤条件的方法,需要将对应的Person对象 * 以参数的形式进行传递 */ /** * 方法表示一个过滤的方法 * @param persons 被过滤的对象 * @return 满足条件的对象的集合 */ List<Person> meetCriteria(List<Person> persons); }
4.编写过滤实现类
package com.js; import java.util.ArrayList; import java.util.List; /** * 男性过滤类 */ public class CriteriaMale implements Criteria{ @Override public List<Person> meetCriteria(List<Person> persons) { /** * male、female */ List<Person> malePersons=new ArrayList<>(); for (Person person : persons){ if (person.getGender().equalsIgnoreCase("male")){ malePersons.add(person); } } return malePersons; } }
package com.js; import java.util.ArrayList; import java.util.List; /** * 女性过滤类 */ public class CriteriaFemale implements Criteria{ @Override public List<Person> meetCriteria(List<Person> persons) { /** * male、female */ List<Person> femalePersons =new ArrayList<>(); for (Person person : persons){ if (person.getGender().equalsIgnoreCase("female")){ femalePersons.add(person); } } return femalePersons; } }
package com.js; 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<>(); for (Person person : persons){ if (person.getMaritalStatus().equalsIgnoreCase("single")){ singlePersons.add(person); } } return singlePersons; } }
package com.js; import java.util.ArrayList; import java.util.List; public class CriteriaMarital implements Criteria{ @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> maritalPersons =new ArrayList<>(); for (Person person : persons){ if (person.getMaritalStatus().equalsIgnoreCase("marital")){ maritalPersons.add(person); } } return maritalPersons; } }
多条件过滤与AND
package com.js; import java.util.ArrayList; 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> firstPersons =new ArrayList<>(); List<Person> secondPersons =new ArrayList<>(); firstPersons = criteria.meetCriteria(persons); secondPersons = otherCriteria.meetCriteria(firstPersons); return secondPersons; } }
多条件过滤或OR
package com.js; import java.util.ArrayList; 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> firstPersons =new ArrayList<>(); List<Person> secondPersons =new ArrayList<>(); firstPersons = criteria.meetCriteria(persons); secondPersons = otherCriteria.meetCriteria(persons); for (Person person : secondPersons){ if (!firstPersons.contains(person)){ firstPersons.add(person); } } return firstPersons; } }
5.创建程序入口
package com.js; import java.util.ArrayList; import java.util.List; public class FilterPatternDemo { public static void main(String[] args) { //构建一个List集合,然后在这个集合中保存不同person信息的对象 List<Person> peoples=new ArrayList<>(); Person p1= new Person("张三","male","single"); Person p2= new Person("李四","female","marital"); Person p3= new Person("王五","female","single"); Person p4= new Person("赵六","male","marital"); Person p5= new Person("张三","male","single"); Person p6= new Person("张三","male","single"); Person p7= new Person("张三","male","marital"); peoples.add(p1); peoples.add(p2); peoples.add(p3); peoples.add(p4); peoples.add(p5); peoples.add(p6); peoples.add(p7); //创建一个过滤器对象(male筛选出来) CriteriaMale male=new CriteriaMale(); List<Person> males = male.meetCriteria(peoples); System.out.println(males); System.out.println("--------------------"); CriteriaSingle single=new CriteriaSingle(); List<Person> singles = single.meetCriteria(peoples); System.out.println(singles);`在这里插入代码片` System.out.println("--------------------"); AndCriteria and=new AndCriteria(male,single); List<Person> ands = and.meetCriteria(peoples); System.out.println(ands); } }
6.结果
[Person{name='张三', gender='male', maritalStatus='single'}, Person{name='赵六', gender='male', maritalStatus='marital'}, Person{name='张三', gender='male', maritalStatus='single'}, Person{name='张三', gender='male', maritalStatus='single'}, Person{name='张三', gender='male', maritalStatus='marital'}] -------------------- [Person{name='张三', gender='male', maritalStatus='single'}, Person{name='王五', gender='female', maritalStatus='single'}, Person{name='张三', gender='male', maritalStatus='single'}, Person{name='张三', gender='male', maritalStatus='single'}] -------------------- [Person{name='张三', gender='male', maritalStatus='single'}, Person{name='张三', gender='male', maritalStatus='single'}, Person{name='张三', gender='male', maritalStatus='single'}]
最后
2022/9/23 14:55
p7
Markdown 6153 字数 363 行数
HTML 5831 字数 237 段落