欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

设计模式之过滤器模式——Java语言描述

程序员文章站 2022-05-03 11:44:26
过滤器模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来 ......

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

实现

创建一个person对象、criteria 接口和实现了该接口的实体类,来过滤 person 对象的列表。criteriapatterndemo,我们的演示类使用 criteria 对象,基于各种标准和它们的结合来过滤 person 对象的列表。

设计模式之过滤器模式——Java语言描述

创建一个person类

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;
   }
   public string getgender() {
      return gender;
   }
   public string getmaritalstatus() {
      return maritalstatus;
   }  
}

为标准创建一个接口

import java.util.list;
 
public interface criteria {
   public list<person> meetcriteria(list<person> persons);
}

创建实体类

创建criteriamale类

import java.util.arraylist;
import java.util.list;
 
public class criteriamale implements criteria {
 
   @override
   public list<person> meetcriteria(list<person> persons) {
      list<person> malepersons = new arraylist<person>(); 
      for (person person : persons) {
         if(person.getgender().equalsignorecase("male")){
            malepersons.add(person);
         }
      }
      return malepersons;
   }
}

创建criteriafemale类

import java.util.arraylist;
import java.util.list;
 
public class criteriafemale implements criteria {
 
   @override
   public list<person> meetcriteria(list<person> persons) {
      list<person> femalepersons = new arraylist<person>(); 
      for (person person : persons) {
         if(person.getgender().equalsignorecase("female")){
            femalepersons.add(person);
         }
      }
      return femalepersons;
   }
}

创建criteriasingle类

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<person>(); 
      for (person person : persons) {
         if(person.getmaritalstatus().equalsignorecase("single")){
            singlepersons.add(person);
         }
      }
      return singlepersons;
   }
}

创建andcriteria类

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> firstcriteriapersons = criteria.meetcriteria(persons);     
      return othercriteria.meetcriteria(firstcriteriapersons);
   }
}

创建orcriteria类

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> firstcriteriaitems = criteria.meetcriteria(persons);
      list<person> othercriteriaitems = othercriteria.meetcriteria(persons);
 
      for (person person : othercriteriaitems) {
         if(!firstcriteriaitems.contains(person)){
           firstcriteriaitems.add(person);
         }
      }  
      return firstcriteriaitems;
   }
}

使用示例

import java.util.arraylist; 
import java.util.list;
 
public class criteriapatterndemo {
   public static void main(string[] args) {
      list<person> persons = new arraylist<person>();
 
      persons.add(new person("robert","male", "single"));
      persons.add(new person("john","male", "married"));
      persons.add(new person("laura","female", "married"));
      persons.add(new person("diana","female", "single"));
      persons.add(new person("mike","male", "single"));
      persons.add(new person("bobby","male", "single"));
 
      criteria male = new criteriamale();
      criteria female = new criteriafemale();
      criteria single = new criteriasingle();
      criteria singlemale = new andcriteria(single, male);
      criteria singleorfemale = new orcriteria(single, female);
 
      system.out.println("males: ");
      printpersons(male.meetcriteria(persons));
 
      system.out.println("\nfemales: ");
      printpersons(female.meetcriteria(persons));
 
      system.out.println("\nsingle males: ");
      printpersons(singlemale.meetcriteria(persons));
 
      system.out.println("\nsingle or females: ");
      printpersons(singleorfemale.meetcriteria(persons));
   }
 
   public static void printpersons(list<person> persons){
      for (person person : persons) {
         system.out.println("person : [ name : " + person.getname() 
            +", gender : " + person.getgender() 
            +", marital status : " + person.getmaritalstatus()
            +" ]");
      }
   }      
}

输出结果

males: 
person : [ name : robert, gender : male, marital status : single ]
person : [ name : john, gender : male, marital status : married ]
person : [ name : mike, gender : male, marital status : single ]
person : [ name : bobby, gender : male, marital status : single ]

females: 
person : [ name : laura, gender : female, marital status : married ]
person : [ name : diana, gender : female, marital status : single ]

single males: 
person : [ name : robert, gender : male, marital status : single ]
person : [ name : mike, gender : male, marital status : single ]
person : [ name : bobby, gender : male, marital status : single ]

single or females: 
person : [ name : robert, gender : male, marital status : single ]
person : [ name : diana, gender : female, marital status : single ]
person : [ name : mike, gender : male, marital status : single ]
person : [ name : bobby, gender : male, marital status : single ]
person : [ name : laura, gender : female, marital status : married ]