Using Filter Design Pattern In Java
How to use filter design pattern in Java—also known as Criteria Design Pattern, which is a structural pattern that combines multiple criteria to obtain single criteria.
Join the DZone community and get the full member experience.Join For Free
Lets discuss another very useful design pattern named - Filter Design Pattern.
Filter Design Pattern
- The Filter Design Pattern also known as Criteria Design Pattern.
- The Filter Design Pattern is a design pattern that allows developers to filter a set of objects using different criteria/filter condition and chaining them in a decoupled way through logical operations.
- The Filter Design Pattern is a structural pattern which can combines multiple criteria to obtain single criteria.
- With the use of Lambda Expressions in Java 8, it is actually very simple to use without even thinking this as a design pattern. But, if we are aware of the concept of filtering, we will have better understanding and command over the implementation of the code.
- There are two different ways of creating filters
- Filter for Entire collection.
- Filter for single member of the collection.
- I will try to highlight use of lambda for filtering as well under this article. But this article is not about Java Lambda Expressions. Based on the viewer's response I may write another article to cover basics of lambdas as well.
To come straight to the Filtering Pattern on a collection of objects, let's use the Employee class I have used in the article of Builder Design Pattern. It's not required to use a builder at all for filtering. All we need is a group of objects to filter. I am using just to save some time in writing a good POJO class to make a collection of and do some good examples of filtering. I hope you understood.
Employee Collection Filter using Filter Design Pattern
Here's the code for the Employee class:
I have added a gender field and also changed depttName to enum for writing filter conditions clearly. So let's define the enums as well.
Here's the code for Gender enum:
Here's the code for Deptt enum:
Example 1: By Using Filter For Entire Collection
In this way we accept the entire collection and return back the collection by eliminating the members which are not applicable as per the filter condition.
Now, lets write an interface named Filter to implement different filter condition or criteria:
Here's the code for MaleFilter class:
Here's the code for EngFilter class:
Lets write two more filter for combining any two filter via 'And Condition' or 'Or Condition'.
Here's the code for AndFilter class:
Here's the code for OrFilter class:
Now lets write Main class to execute and test the output:
Below is the output:
Example 2: By Using Filter For Single Member of the Collection
In this way we accept a member of the collection in the filter and validate it to know whether the member is satisfying the filter condition or criteria or not.
Here's the code for Filter interface:
Here's the code for MaleFilter class:
Here's the code for MinSalaryFilter class:
Similarly, Here's the for AndFilter class:
And here's the code for OrFilter class:
Simpler! Well yes it is since the collection traversing is moved to client application (Main class in our example)
Now lets write our Main class to execute and test the output:
And below is the output:
You know what? by using Java 8 or higher, we even do not need to define our filter at all and can create filter conditions by using Java Predicate as we did in our Main while using lambdas.
Well, that all! I hope this tutorial helped to understand the Filter pattern.
Source Code can be found here: Filter-Design-Pattern-Source-Code
Let me know if you like to have an article on Lambda expressions as well? I will try to write the one if you want. :)
Liked the article? Please don't forget to press that like button. Happy coding!
Need more articles on Design Patterns? Please visit my profile to find more: Brijesh Saxena
Opinions expressed by DZone contributors are their own.