优秀网站设计案例分析ppt,没有网站做分类信息群发,个人主页网站应该怎样做,绍兴做网站公司哪家好Java设计模式 - 过滤器/标准模式 过滤器模式使用不同的条件过滤对象。
这些标准可以通过逻辑操作链接在一起。
过滤器模式是一种结构型模式。
例子
import java.util.List;
import java.util.ArrayList;class Employee {private String name;private String gender;private…Java设计模式 - 过滤器/标准模式 过滤器模式使用不同的条件过滤对象。
这些标准可以通过逻辑操作链接在一起。
过滤器模式是一种结构型模式。
例子
import java.util.List;
import java.util.ArrayList;class Employee {private String name;private String gender;private String retireStatus;public Employee(String name, String gender, String r) {this.name name;this.gender gender;this.retireStatus r;}public String getName() {return name;}public String getGender() {return gender;}public String getRetireStatus() {return retireStatus;}Overridepublic String toString() {return Employee [name name , gender gender , retireStatus retireStatus ];}
}interface Criteria {public ListEmployee meetCriteria(ListEmployee persons);
}class CriteriaMale implements Criteria {Overridepublic ListEmployee meetCriteria(ListEmployee persons) {ListEmployee malePersons new ArrayListEmployee();for (Employee person : persons) {if (person.getGender().equalsIgnoreCase(MALE)) {malePersons.add(person);}}return malePersons;}
}class CriteriaFemale implements Criteria {Overridepublic ListEmployee meetCriteria(ListEmployee persons) {ListEmployee femalePersons new ArrayListEmployee();for (Employee person : persons) {if (person.getGender().equalsIgnoreCase(FEMALE)) {femalePersons.add(person);}}return femalePersons;}
}class CriteriaRetire implements Criteria {Overridepublic ListEmployee meetCriteria(ListEmployee persons) {ListEmployee singlePersons new ArrayListEmployee();for (Employee person : persons) {if (person.getRetireStatus().equalsIgnoreCase(YES)) {singlePersons.add(person);}}return singlePersons;}
}class AndCriteria implements Criteria {private Criteria criteria;private Criteria otherCriteria;public AndCriteria(Criteria criteria, Criteria otherCriteria) {this.criteria criteria;this.otherCriteria otherCriteria;}Overridepublic ListEmployee meetCriteria(ListEmployee persons) {ListEmployee firstCriteriaPersons criteria.meetCriteria(persons);return otherCriteria.meetCriteria(firstCriteriaPersons);}
}class OrCriteria implements Criteria {private Criteria criteria;private Criteria otherCriteria;public OrCriteria(Criteria criteria, Criteria otherCriteria) {this.criteria criteria;this.otherCriteria otherCriteria;}Overridepublic ListEmployee meetCriteria(ListEmployee persons) {ListEmployee firstCriteriaItems criteria.meetCriteria(persons);ListEmployee otherCriteriaItems otherCriteria.meetCriteria(persons);for (Employee person : otherCriteriaItems) {if (!firstCriteriaItems.contains(person)) {firstCriteriaItems.add(person);}}return firstCriteriaItems;}
}public class Main {public static void main(String[] args) {ListEmployee persons new ArrayListEmployee();persons.add(new Employee(Tom, Male, YES));persons.add(new Employee(Jack, Male, NO));persons.add(new Employee(Jane, Female, NO));persons.add(new Employee(Diana, Female, YES));persons.add(new Employee(Mike, Male, NO));persons.add(new Employee(Bob, Male, YES));Criteria male new CriteriaMale();Criteria female new CriteriaFemale();Criteria retire new CriteriaRetire();Criteria retireMale new AndCriteria(retire, male);Criteria retireOrFemale new OrCriteria(retire, female);System.out.println(Males: );printPersons(male.meetCriteria(persons));System.out.println(Females: );printPersons(female.meetCriteria(persons));System.out.println(Retire Males: );printPersons(retireMale.meetCriteria(persons));System.out.println(Retire Or Females: );printPersons(retireOrFemale.meetCriteria(persons));}public static void printPersons(ListEmployee persons) {for (Employee person : persons) {System.out.println(person);}}
}上面的代码生成以下结果。