设计模式-过滤器模式

过滤器模式简介

定义:是一种结构性模式,允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来

目标:将传递过来的数据进行分类处理

 

实现

 

代码:

  1 /**实体类**/
  2 public class Person {
  3     private String name;
  4     private String gender;
  5     private String maritalStatus;
  6      
  7     public Person(String name,String gender,String maritalStatus){
  8         this.name = name;
  9         this.gender = gender;
 10         this.maritalStatus = maritalStatus;    
 11      }
 12      
 13      public String getName() {
 14         return name;
 15      }
 16      public String getGender() {
 17         return gender;
 18      }
 19      public String getMaritalStatus() {
 20         return maritalStatus;
 21      }
 22 }
 23 
 24 /**过滤接口**/
 25 public interface Filter {
 26     public List<Person> meetCriteria(List<Person> persons);
 27 }
 28 
 29 /**具体过滤类1**/
 30 public class FilterMale implements Filter{
 31 
 32     public List<Person> meetCriteria(List<Person> persons) {
 33         // TODO Auto-generated method stub
 34         List<Person> malePersons = new ArrayList<Person>(); 
 35           for (Person person : persons) {
 36              if(person.getGender().equalsIgnoreCase("MALE")){
 37                 malePersons.add(person);
 38              }
 39           }
 40           return malePersons;
 41     }
 42 
 43 }
 44 
 45 /**具体过滤类2**/
 46 public class FilterFemale implements Filter{
 47 
 48     public List<Person> meetCriteria(List<Person> persons) {
 49         // TODO Auto-generated method stub
 50         List<Person> malePersons = new ArrayList<Person>(); 
 51           for (Person person : persons) {
 52              if(person.getGender().equalsIgnoreCase("FEMALE")){
 53                 malePersons.add(person);
 54              }
 55           }
 56           return malePersons;
 57     }
 58 
 59 }
 60 
 61 /**具体过滤类3**/
 62 public class FilterSingle implements Filter{
 63 
 64     public List<Person> meetCriteria(List<Person> persons) {
 65         // TODO Auto-generated method stub
 66         List<Person> malePersons = new ArrayList<Person>(); 
 67           for (Person person : persons) {
 68              if(person.getGender().equalsIgnoreCase("SINGLE")){
 69                 malePersons.add(person);
 70              }
 71           }
 72           return malePersons;
 73     }
 74 
 75 }
 76 
 77 /**测试类**/
 78 public class App {
 79     public static void printPersons(List<Person> persons){
 80           for (Person person : persons) {
 81              System.out.println("Person : [ Name : " + person.getName() 
 82                 +", Gender : " + person.getGender() 
 83                 +", Marital Status : " + person.getMaritalStatus()
 84                 +" ]");
 85           }
 86     }
 87     public static void main( String[] args ){
 88         List<Person> persons = new ArrayList<Person>();
 89         persons.add(new Person("Robert","Male", "Single"));
 90         persons.add(new Person("John","Male", "Married"));
 91         persons.add(new Person("Laura","Female", "Married"));
 92         persons.add(new Person("Diana","Female", "Single"));
 93         persons.add(new Person("Mike","Male", "Single"));
 94         persons.add(new Person("Bobby","SINGLE", "Single"));
 95         
 96         Filter male = new FilterMale();
 97         Filter female = new FilterFemale();
 98         Filter single = new FilterSingle();
 99    
100         System.out.println("Males: ");
101         printPersons(male.meetCriteria(persons));
102    
103         System.out.println("\nFemales: ");
104         printPersons(female.meetCriteria(persons));
105         
106         System.out.println("\nSingle: ");
107         printPersons(single.meetCriteria(persons));
108     }
109     
110     
111 }
112 
113 测试结果:
114 Males: 
115 Person : [ Name : Robert, Gender : Male, Marital Status : Single ]
116 Person : [ Name : John, Gender : Male, Marital Status : Married ]
117 Person : [ Name : Mike, Gender : Male, Marital Status : Single ]
118 
119 Females: 
120 Person : [ Name : Laura, Gender : Female, Marital Status : Married ]
121 Person : [ Name : Diana, Gender : Female, Marital Status : Single ]
122 
123 Single: 
124 Person : [ Name : Bobby, Gender : SINGLE, Marital Status : Single ]

 

posted @ 2020-12-16 17:16  CarBlack  阅读(158)  评论(0编辑  收藏  举报