Java进阶篇设计模式之六 ----- 组合模式和过滤器模式 (2)

然后再实现该接口,制定不同的过滤规则。这里主要是三种规则,普通的过滤,且过滤,或过滤。
具体实现的方法如下:

class MaleStudents implements FilterinGrule{ @Override public List<Student> filter(List<Student> students) { List<Student> maleStudents = new ArrayList<Student>(); students.forEach(student->{ if(student.getGender().equalsIgnoreCase("male")){ maleStudents.add(student); } }); return maleStudents; } } class FemaleStudents implements FilterinGrule{ @Override public List<Student> filter(List<Student> students) { List<Student> femaleStudents = new ArrayList<Student>(); students.forEach(student->{ if(student.getGender().equalsIgnoreCase("female")){ femaleStudents.add(student); } }); return femaleStudents; } } class SecondGrade implements FilterinGrule{ @Override public List<Student> filter(List<Student> students) { List<Student> secondGradeStudents = new ArrayList<Student>(); students.forEach(student->{ if(student.getGrade() == 2){ secondGradeStudents.add(student); } }); return secondGradeStudents; } } class And implements FilterinGrule{ private FilterinGrule filter; private FilterinGrule filter2; public And(FilterinGrule filter,FilterinGrule filter2) { this.filter=filter; this.filter2=filter2; } @Override public List<Student> filter(List<Student> students) { List<Student> students2=filter.filter(students); return filter2.filter(students2); } } class Or implements FilterinGrule{ private FilterinGrule filter; private FilterinGrule filter2; public Or(FilterinGrule filter,FilterinGrule filter2) { this.filter=filter; this.filter2=filter2; } @Override public List<Student> filter(List<Student> students) { List<Student> students1=filter.filter(students); List<Student> students2=filter2.filter(students); students2.forEach(student->{ if(!students1.contains(student)){ students1.add(student); } }); return students1; } }

最后再来进行调用测试,添加一些学生,并且指定性别以及班级。然后根据不同的条件来进行过滤。

public class FilterTest { public static void main(String[] args) { List<Student> list=new ArrayList<Student>(); list.add(new Student("小明", "male", 1)); list.add(new Student("小红", "female", 2)); list.add(new Student("小刚", "male", 2)); list.add(new Student("小霞", "female", 3)); list.add(new Student("小智", "male", 3)); list.add(new Student("虚无境", "male", 1)); FilterinGrule male = new MaleStudents(); FilterinGrule female = new FemaleStudents(); FilterinGrule secondGrade = new SecondGrade(); FilterinGrule secondGradeMale = new And(secondGrade, male); FilterinGrule secondGradeOrFemale = new Or(secondGrade, female); System.out.println("男生:"+male.filter(list)); System.out.println("女生:"+female.filter(list)); System.out.println("二年级学生:"+secondGrade.filter(list)); System.out.println("二年级男生:"+secondGradeMale.filter(list)); System.out.println("二年级的学生或女生:"+secondGradeOrFemale.filter(list)); } }

输出结果:

男生:[Student [name=小明, gender=male, grade=1], Student [name=小刚, gender=male, grade=2], Student [name=小智, gender=male, grade=3], Student [name=虚无境, gender=male, grade=1]] 女生:[Student [name=小红, gender=female, grade=2], Student [name=小霞, gender=female, grade=3]] 二年级学生:[Student [name=小红, gender=female, grade=2], Student [name=小刚, gender=male, grade=2]] 二年级男生:[Student [name=小刚, gender=male, grade=2]] 二年级的学生或女生:[Student [name=小红, gender=female, grade=2], Student [name=小刚, gender=male, grade=2], Student [name=小霞, gender=female, grade=3]]

通过上述示例,我们发现过滤器模式其实很简单,制定过滤规则,然后再根据制定的标准来进行过滤,得到符合条件的数据。过滤器模式虽然简单,但是在构建过滤规则的时候,有点繁琐,不过在jdk1.8之后,我们可以使用stream流更方便的进行规则的制定(这一点留在以后再讲)。

过滤器模式的优点:

简单,解耦,使用方便。

过滤器模式的缺点:

好像没有。。。

使用场景:

需要进行筛选的时候。

其它 音乐推荐

其实边听音乐便看博客很享受的~_~

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyfjpp.html