Using conditions to select desired objects with the filter pattern
The filter pattern – sometimes called the criteria pattern – is a design pattern that allows clients to filter a set of objects using different criteria, or rules, and chain them separately using logical operations.
Motivation
The filter pattern helps simplify the code base to work like container objects that use subtyping instead of parameterization (generics) for an extensible class structure. It allows the client to easily extend and expose the filtering capability of container-like objects. Different filtering conditions can be dynamically added or removed without notifying the client.
Finding it in the JDK
Let us consider a filter as an interface with a single function and a logical Boolean result. A nice example of the filter pattern is the Predicate
class, found in the java.base
module and the java.util.function
package. Predicate
represents a Boolean function and is intended for use...