Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Java filter mode

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to implement the Java filter mode". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to implement the Java filter mode"!

Filter pattern (Filter Pattern) or standard pattern (Criteria Pattern) is a design pattern that allows developers to filter a set of objects using different standards and connect them in a decoupled manner through logical operations. This type of design pattern belongs to structural pattern, which combines multiple standards to obtain a single standard.

Realize

We will create a Person object, a Criteria interface, and an entity class that implements that interface to filter the list of Person objects. CriteriaPatternDemo, our demo class uses Criteria objects to filter the list of Person objects based on various standards and their combinations.

Step 1

Create a class and apply standards to it.

Person.javapublic 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;}} step 2

Create an interface for the standard (Criteria).

Criteria.javaimport java.util.List;public interface Criteria {public List meetCriteria (List persons);} step 3

Create an entity class that implements the Criteria interface.

CriteriaMale.javaimport java.util.ArrayList;import java.util.List;public class CriteriaMale implements Criteria {@ Override public List meetCriteria (List persons) {List malePersons = new ArrayList (); for (Person person: persons) {if (person.getGender (). EqualsIgnoreCase ("MALE")) {malePersons.add (person);}} return malePersons;}} CriteriaFemale.javaimport java.util.ArrayList;import java.util.List Public class CriteriaFemale implements Criteria {@ Override public List meetCriteria (List persons) {List femalePersons = new ArrayList (); for (Person person: persons) {if (person.getGender (). EqualsIgnoreCase ("FEMALE")) {femalePersons.add (person);}} return femalePersons;}} CriteriaSingle.javaimport java.util.ArrayList;import java.util.List;public class CriteriaSingle implements Criteria {@ Override public List meetCriteria (List persons) {List singlePersons = new ArrayList () For (Person person: persons) {if (person.getMaritalStatus (). EqualsIgnoreCase ("SINGLE")) {singlePersons.add (person);}} return singlePersons;}} AndCriteria.javaimport 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 meetCriteria (List persons) {List firstCriteriaPersons = criteria.meetCriteria (persons); return otherCriteria.meetCriteria (firstCriteriaPersons);}} OrCriteria.javaimport 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 meetCriteria (List persons) {List firstCriteriaItems = criteria.meetCriteria (persons); List otherCriteriaItems = otherCriteria.meetCriteria (persons) For (Person person: otherCriteriaItems) {if (! firstCriteriaItems.contains (person)) {firstCriteriaItems.add (person);}} return firstCriteriaItems;}} step 4

Use different standards (Criteria) and their combination to filter the list of Person objects.

CriteriaPatternDemo.javaimport java.util.ArrayList;import java.util.List;public class CriteriaPatternDemo {public static void main (String [] args) {List persons = new ArrayList (); 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 persons) {for (Person person: persons) {System.out.println ("Person: [Name:" + person.getName () + ", Gender:" + person.getGender () + ", MaritalStatus:" + person.getMaritalStatus () + "]");}} step 5

Execute the program and output the result:

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: Marital Status] Marital Status: Single: [Single: Person, Person: Person, Person: Person] [:,:,:] I believe you have a deeper understanding of "how to implement the Java filter mode". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report