过滤器模式:
过滤器模式或标准模式是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式吧他们连接起来,这种类型的设计模式属于结构型模式。
实现:
将创建一个Person对象、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;
}
}
2.为标准(Criteria)创建一个接口
Criteria.java
public interface Criteria{
public List<Person> meetCriteria(List<Person> persons);
}
3.创建实现了Criteria接口的实体类
CriteriaMale.java
public class CriteriaMale implements Criteria{
public List<Person> meetCirteria(List<Person> persons){
List<Person> malPersons=new ArrayList<Person>();
for(Person person:persons){
if(person.getGender().equalsIgnoreCase("MALE")){
malePerons.add(person);
}
}
return malePersons;
}
}
定义过滤的模式来进行筛选,过滤器模式
本文介绍了一种设计模式——过滤器模式,它允许开发者使用不同标准过滤对象集合,并通过逻辑运算以解耦方式连接这些标准。文章通过具体示例展示了如何实现这一模式。
1080

被折叠的 条评论
为什么被折叠?



