You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: filterer/README.md
+72-37Lines changed: 72 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,8 @@ tag:
6
6
- Data processing
7
7
- Data transformation
8
8
- Decoupling
9
+
- Functional decomposition
10
+
- Object composition
9
11
- Performance
10
12
- Runtime
11
13
---
@@ -21,49 +23,44 @@ The Filterer pattern aims to apply a series of filters to data objects, where ea
21
23
22
24
## Explanation
23
25
24
-
Realworld example
26
+
Real-world example
25
27
26
-
> We are designing a threat (malware) detection software which can analyze target systems for threats that are present in it. In the design we have to take into consideration that new Threat types can be added later. Additionally, there is a requirement that the threat detection system can filter the detected threats based on different criteria (the target system acts as container-like object for threats).
28
+
> Imagine a library that needs to filter books based on different criteria such as genre, author, publication year, or availability. Instead of writing separate methods for each possible combination of criteria, the library system employs the Filterer design pattern. Each filter criterion is encapsulated as an object, and these filter objects can be combined dynamically at runtime to create complex filtering logic. For example, a user can search for books that are both available and published after 2010 by combining the availability filter and the publication year filter. This approach makes the system more flexible and easier to maintain, as new filtering criteria can be added without modifying existing code.
27
29
28
30
In plain words
29
31
30
32
> Filterer pattern is a design pattern that helps container-like objects return filtered versions of themselves.
31
33
32
34
**Programmatic Example**
33
35
34
-
To model the threat detection example presented above we introduce `Threat` and `ThreatAwareSystem` interfaces.
36
+
We are designing a threat (malware) detection software which can analyze target systems for threats that are present in it. In the design we have to take into consideration that new Threat types can be added later. Additionally, there is a requirement that the threat detection system can filter the detected threats based on different criteria (the target system acts as container-like object for threats).
37
+
38
+
To model the threat detection system, we introduce `Threat` and `ThreatAwareSystem` interfaces.
Notice the `filtered` method that returns instance of `Filterer` interface which is defined as:
55
55
56
56
```java
57
-
58
57
@FunctionalInterface
59
58
publicinterfaceFilterer<G, E> {
60
59
Gby(Predicate<? super E>predicate);
61
60
}
62
61
```
63
62
64
-
It is used to fulfill the requirement for system to be able to filter itself based on threat properties. The container-like object (`ThreatAwareSystem` in our case) needs to have a method that returns an instance of `Filterer`. This helper interface gives ability to covariantly specify a lower bound of contravariant `Predicate` in the subinterfaces of interfaces representing the container-like objects.
65
-
66
-
In our example we will be able to pass a predicate that takes `? extends Threat` object and return `? extends ThreatAwareSystem` from `Filtered::by` method. A simple implementation of `ThreatAwareSystem`:
Notice how we override the `filtered` method in `ProbabilisticThreatAwareSystem` and specify different return covariant type by specifying different generic types. Our interfaces are clean and not cluttered by default implementations. We will be able to filter `ProbabilisticThreatAwareSystem` by `ProbableThreat` properties:
122
+
We will be able to filter `ProbabilisticThreatAwareSystem` by `ProbableThreat` properties:
This pattern is useful in scenarios where data needs to be processed in discrete steps, and each step's output is the input for the next step. Common in stream processing, audio/video processing pipelines, or any data processing applications requiring staged transformations.
231
+
* Use when you need to filter a collection of objects dynamically based on different criteria.
232
+
* Suitable for applications where filtering logic changes frequently or needs to be combined in various ways.
233
+
* Ideal for scenarios requiring separation of filtering logic from the core business logic.
199
234
200
235
## Tutorials
201
236
202
-
*[Article about Filterer pattern posted on its author's blog](https://blog.tlinkowski.pl/2018/filterer-pattern/)
203
-
*[Application of Filterer pattern in domain of text analysis](https://www.javacodegeeks.com/2019/02/filterer-pattern-10-steps.html)
*[Filterer Pattern in 10 Steps (Java Code Geeks)](https://www.javacodegeeks.com/2019/02/filterer-pattern-10-steps.html)
204
239
205
240
## Known Uses
206
241
@@ -215,15 +250,15 @@ Benefits:
215
250
* Enhances testability, as filters can be tested independently.
216
251
* Promotes loose coupling between the stages of data processing.
217
252
218
-
## Trade-offs:
253
+
Trade-offs:
219
254
220
255
* Potential performance overhead from continuous data passing between filters.
221
256
* Complexity can increase with the number of filters, potentially affecting maintainability.
222
257
223
258
## Related Patterns
224
259
225
-
* Chain of Responsibility: Filters can be seen as a specialized form of the Chain of Responsibility, where each filter decides if and how to process the input data and whether to pass it along the chain.
226
-
* Decorator: Similar to Decorator in that both modify behavior dynamically; however, filters focus more on data transformation than on adding responsibilities.
260
+
*[Chain of Responsibility](https://java-design-patterns.com/patterns/chain-of-responsibility/): Filters can be seen as a specialized form of the Chain of Responsibility, where each filter decides if and how to process the input data and whether to pass it along the chain.
261
+
*[Decorator](https://java-design-patterns.com/patterns/decorator/): Similar to Decorator in that both modify behavior dynamically; however, filters focus more on data transformation than on adding responsibilities.
0 commit comments