过滤器模式 rust和java的实现(一)https://developer.aliyun.com/article/1392026
rust
由于时间关系,并没有实现or条件,大家有兴趣可以自行补充
// 设置人类实体类 #[derive(Clone)] struct Person{ name:String, gender:String, marital_status:String } impl Person { fn get_gender(&self)->&str { self.gender.as_ref() } fn get_marital_status(&self)->&str { self.marital_status.as_ref() } } // 设置过滤标准特征 trait Criteria { fn meet_criteria(&self,persons:Vec<Person>)->Vec<Person>; } // 设置男性 struct CriteriaMale{} // 重写男性评判标准 impl Criteria for CriteriaMale { fn meet_criteria(&self,persons:Vec<Person>)->Vec<Person>{ persons.into_iter().filter(|x| x.get_gender().eq_ignore_ascii_case("MALE")).collect() } } // 设置女性 struct CriteriaFemale{} // 设置女性标准 impl Criteria for CriteriaFemale{ fn meet_criteria(&self,persons:Vec<Person>)->Vec<Person>{ persons.into_iter().filter(|x| x.get_gender().eq_ignore_ascii_case("FEMALE")).collect() } } // 设置单身标准 struct CriteriaSingle{} impl Criteria for CriteriaSingle{ fn meet_criteria(&self,persons:Vec<Person>)->Vec<Person>{ persons.into_iter().filter(|x| x.get_marital_status().eq_ignore_ascii_case("SINGLE")).collect() } } // 设置and标准,求交集 struct AndCriteria { criteria:Box<dyn Criteria>, other_criteria:Box<dyn Criteria> } impl Criteria for AndCriteria { fn meet_criteria(&self,persons:Vec<Person>)->Vec<Person> { self.other_criteria.meet_criteria(self.criteria.meet_criteria(persons)) } } // struct OrCriteria { // criteria:Box<dyn Criteria>, // other_criteria:Box<dyn Criteria> // } // impl Criteria for OrCriteria{ // fn meet_criteria(&self,persons:Vec<Person>)->Vec<Person> { // let mut first_criteria_items = self.criteria.meet_criteria(persons); // let other_criteria_items = self.other_criteria.meet_criteria(persons); // for o in other_criteria_items { // if !other_criteria_items.contains(&o) { // first_criteria_items.push(o); // } // } // first_criteria_items // } // } fn print_persons(persons:Vec<Person>) { persons.into_iter().for_each(|x| println!("Person : [NAME: {},Gender : {} ,Maeital Status : {} ]",x.name,x.gender,x.marital_status)); } fn main() { let mut persons=Vec::new(); persons.push(Person{name:String::from("Robert"),gender:String::from("Male"),marital_status:String::from("Single")}); persons.push(Person{name:String::from("John"),gender:String::from("Male"),marital_status:String::from("Married")}); persons.push(Person{name:String::from("Laura"),gender:String::from("Female"),marital_status:String::from("Married")}); persons.push(Person{name:String::from("Diana"),gender:String::from("Female"),marital_status:String::from("Single")}); let male=Box::new(CriteriaMale{}); let female=Box::new(CriteriaFemale{}); let single_male=AndCriteria{criteria:Box::new(CriteriaSingle{}),other_criteria:Box::new(CriteriaMale{})}; // let single_or_female=OrCriteria{criteria:Box::new(CriteriaMale{}),other_criteria:Box::new(CriteriaFemale{})}; println!("男士"); print_persons(male.meet_criteria(persons.clone())); println!("女士"); print_persons(female.meet_criteria(persons.clone())); println!("单身男士"); print_persons(single_male.meet_criteria(persons)) }
java
步骤 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; } public String getGender() { return gender; } public String getMaritalStatus() { return maritalStatus; } }
步骤 2
为标准(Criteria)创建一个接口。
Criteria.java
import java.util.List; public interface Criteria { public List<Person> meetCriteria(List<Person> persons); }
步骤 3
创建实现了 Criteria 接口的实体类。
CriteriaMale.java
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.java
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.java
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.java
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.java
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; } }
步骤4
使用不同的标准(Criteria)和它们的结合来过滤 Person 对象的列表。
CriteriaPatternDemo.java
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() +" ]"); } } }
步骤 5
执行程序,输出结果:
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 ]
rust
由于时间关系,并没有实现or条件,大家有兴趣可以自行补充
// 设置人类实体类 #[derive(Clone)] struct Person{ name:String, gender:String, marital_status:String } impl Person { fn get_gender(&self)->&str { self.gender.as_ref() } fn get_marital_status(&self)->&str { self.marital_status.as_ref() } } // 设置过滤标准特征 trait Criteria { fn meet_criteria(&self,persons:Vec<Person>)->Vec<Person>; } // 设置男性 struct CriteriaMale{} // 重写男性评判标准 impl Criteria for CriteriaMale { fn meet_criteria(&self,persons:Vec<Person>)->Vec<Person>{ persons.into_iter().filter(|x| x.get_gender().eq_ignore_ascii_case("MALE")).collect() } } // 设置女性 struct CriteriaFemale{} // 设置女性标准 impl Criteria for CriteriaFemale{ fn meet_criteria(&self,persons:Vec<Person>)->Vec<Person>{ persons.into_iter().filter(|x| x.get_gender().eq_ignore_ascii_case("FEMALE")).collect() } } // 设置单身标准 struct CriteriaSingle{} impl Criteria for CriteriaSingle{ fn meet_criteria(&self,persons:Vec<Person>)->Vec<Person>{ persons.into_iter().filter(|x| x.get_marital_status().eq_ignore_ascii_case("SINGLE")).collect() } } // 设置and标准,求交集 struct AndCriteria { criteria:Box<dyn Criteria>, other_criteria:Box<dyn Criteria> } impl Criteria for AndCriteria { fn meet_criteria(&self,persons:Vec<Person>)->Vec<Person> { self.other_criteria.meet_criteria(self.criteria.meet_criteria(persons)) } } // struct OrCriteria { // criteria:Box<dyn Criteria>, // other_criteria:Box<dyn Criteria> // } // impl Criteria for OrCriteria{ // fn meet_criteria(&self,persons:Vec<Person>)->Vec<Person> { // let mut first_criteria_items = self.criteria.meet_criteria(persons); // let other_criteria_items = self.other_criteria.meet_criteria(persons); // for o in other_criteria_items { // if !other_criteria_items.contains(&o) { // first_criteria_items.push(o); // } // } // first_criteria_items // } // } fn print_persons(persons:Vec<Person>) { persons.into_iter().for_each(|x| println!("Person : [NAME: {},Gender : {} ,Maeital Status : {} ]",x.name,x.gender,x.marital_status)); } fn main() { let mut persons=Vec::new(); persons.push(Person{name:String::from("Robert"),gender:String::from("Male"),marital_status:String::from("Single")}); persons.push(Person{name:String::from("John"),gender:String::from("Male"),marital_status:String::from("Married")}); persons.push(Person{name:String::from("Laura"),gender:String::from("Female"),marital_status:String::from("Married")}); persons.push(Person{name:String::from("Diana"),gender:String::from("Female"),marital_status:String::from("Single")}); let male=Box::new(CriteriaMale{}); let female=Box::new(CriteriaFemale{}); let single_male=AndCriteria{criteria:Box::new(CriteriaSingle{}),other_criteria:Box::new(CriteriaMale{})}; // let single_or_female=OrCriteria{criteria:Box::new(CriteriaMale{}),other_criteria:Box::new(CriteriaFemale{})}; println!("男士"); print_persons(male.meet_criteria(persons.clone())); println!("女士"); print_persons(female.meet_criteria(persons.clone())); println!("单身男士"); print_persons(single_male.meet_criteria(persons)) }
rust代码仓库
https://github.com/onenewcode/design.git
本教程项目在bin文件夹下的filtert.rs文件中