Filter Pattern
The Filter Pattern, or Criteria Pattern, is a design pattern that allows developers to filter a set of objects using different criteria, connecting them in a decoupled way through logical operations. This type of design pattern belongs to the structural patterns; it combines multiple criteria to obtain a single criterion.
Summary
Intent
Used to encapsulate the filtering process of objects, allowing objects to be filtered dynamically using different filtering criteria.
Main problem solved
When a set of objects needs to be filtered according to several different conditions or criteria, the Filter Pattern provides a flexible way to define these conditions and avoids hardcoding filtering logic in the client code.
Use Cases
- When a collection of objects needs to be filtered according to different criteria.
- When the filtering logic may change, or when multiple filtering conditions need to be combined dynamically.
Implementation
- Define the filtering interface: create a filtering interface that defines a filtering method.
- Implement concrete filters: implement the filtering interface for each filtering criterion, encapsulating the specific filtering logic.
- Combine filters: allow filters to be combined to form complex filtering logic.
Key code
- Filtering interface: defines the filtering method, such as
matches()。 - Concrete filter classes: implement the filtering interface and encapsulate the specific filtering logic.
- Combine filters: implements the combination logic of filters, such as logical AND, logical OR, and so on.
Application Example
- Library management system: filter books by different criteria such as author, publication year, and category.
- Online shopping platform: filter products by conditions such as price, brand, and user rating.
Advantages
- Encapsulation: the filtering logic is encapsulated in independent filter objects.
- Flexibility: filtering conditions can be added, modified, or combined dynamically.
- Extensibility: new filtering criteria can be added easily without modifying existing code.
Disadvantages
- Complexity: as the number of filtering conditions increases, the system may become complex.
- Performance Issues: if the combination of filters is too complex, performance may be affected.
Usage recommendation
- When the filtering logic may change or objects need to be filtered dynamically by different criteria, consider using the Filter Pattern.
- When designing, ensure that the interfaces and implementations of the filters are consistent to facilitate combination and extension.
Notes
- Ensure that the combination logic of the filters is correct to avoid introducing logical errors.
- When implementing, consider the performance impact, especially when handling large amounts of data.
Summary
The Filter Pattern contains the following main roles:
-
Filter interface (Filter/Criteria): defines an interface for filtering objects. This interface usually contains a method for filtering objects according to a specific condition.
-
Concrete Filter class (Concrete Filter/Concrete Criteria): implements the filter interface and specifically defines the conditions and logic for filtering objects.
-
Collection of objects (Items/Objects to be filtered): the collection of objects to be filtered. These objects are usually instances with common attributes, such as a group of people or a group of products.
-
Client: use the concrete filter classes to filter the collection of objects. The client combines the collection of objects and the filters to obtain the objects that meet the conditions.
Create a class that implements
We will create a Person objects,Criteria interface and the entity classes that implement it, to filter Person list of objects.CriteriaPatternDemo Class uses Criteria objects, based on various criteria and their combinations to filter Person list of objects.
Step 1
Create a class on which the criteria are applied.
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
;
}
}
Step 2
Create an interface for the Criteria.
Criteria.java
import
java
.
util
.
List
;
public
interface
Criteria
{
public
List
<
Person
meetCriteria
(
List
<
Person
persons
);
}
Step 3
Create concrete classes that implement the Criteria interface.
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
;
}
}
Step 4
Use different Criteria and their combinations to filter Person list of objects.
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
(
"
\n
Females:
"
)
;
printPersons
(
female
.
meetCriteria
(
persons
)
)
;
System
.
out
.
println
(
"
\n
Single Males:
"
)
;
printPersons
(
singleMale
.
meetCriteria
(
persons
)
)
;
System
.
out
.
println
(
"
\n
Single 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
(
)
"
]
"
)
;
}
}
}
Step 5
Run the program, output 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 : Single ]
Person : [ Name : Bobby, Gender : Male, Marital Status : Single ]
Person : [ Name : Laura, Gender : Female, Marital Status : Married ]