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
{{ message }}
Repository navigation
Commit 015b418
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: filterer/README.md
+66-31Lines changed: 66 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,27 +11,33 @@ tags:
11
11
---
12
12
13
13
## Name / classification
14
+
14
15
Filterer
15
16
16
17
## Intent
17
-
The intent of this design pattern is to introduce a functional interface that will add a functionality for container-like objects to easily return filtered versions of themselves.
18
+
19
+
The intent of this design pattern is to introduce a functional interface that will add a
20
+
functionality for container-like objects to easily return filtered versions of themselves.
18
21
19
22
## Explanation
23
+
20
24
Real world example
21
25
22
-
> We are designing a threat(malware) detection system. We can have different types of threats and systems. We have a requirement that
23
-
> system should be aware of threats that are present in it. In the design we have to take into consideration that new Threat types can be
24
-
> added later. Also there is a requirement that a system can filter itself based on the threats that it possesses (system acts as container-like object for threats).
25
-
>
26
+
> We are designing a threat (malware) detection software which can analyze target systems for
27
+
> threats that are present in it. In the design we have to take into consideration that new
28
+
> Threat types can be added later. Additionally, there is a requirement that the threat detection
29
+
> system can filter the detected threats based on different criteria (the target system acts as
30
+
> container-like object for threats).
26
31
27
32
In plain words
28
33
29
-
> We need to be able to filter different types of systems(container-like objects) based on properties of Threats that they contain.
30
-
> Adding new properties for Threats should be easy (we still need the ability to filter by those new properties).
34
+
> Filterer pattern is a design pattern that helps container-like objects return filtered versions
35
+
> of themselves.
31
36
32
37
**Programmatic Example**
33
38
34
-
To model the threat detection example presented above we introduce `Threat` and `ThreatAwareSystem` interfaces.
39
+
To model the threat detection example presented above we introduce `Threat` and `ThreatAwareSystem`
40
+
interfaces.
35
41
36
42
```java
37
43
publicinterfaceThreat {
@@ -47,19 +53,26 @@ public interface ThreatAwareSystem {
47
53
48
54
}
49
55
```
50
-
Notice the `filtered` method that returns instance of `Filterer` interface which is defined as :
56
+
57
+
Notice the `filtered` method that returns instance of `Filterer` interface which is defined as:
58
+
51
59
```java
52
60
@FunctionalInterface
53
61
publicinterfaceFilterer<G, E> {
54
62
Gby(Predicate<? super E>predicate);
55
63
}
56
64
```
57
-
it is used to fulfill the requirement for system to be able to filter itself based on threat properties.
58
-
The container-like object (`ThreatAwareSystem` in our case) needs to have a method that returns an instance of `Filterer`. This helper interface gives
59
-
ability to covariantly specify a lower bound of contravariant `Predicate` in the subinterfaces of interfaces representing the container-like objects.
60
65
61
-
In our example we will be able to pass a predicate that takes `? extends Threat` object and return `? extends ThreatAwareSystem`
62
-
from `Filtered::by` method. A simple implementation of `ThreatAwareSystem` :
66
+
It is used to fulfill the requirement for system to be able to filter itself based on threat
67
+
properties. The container-like object (`ThreatAwareSystem` in our case) needs to have a method that
68
+
returns an instance of `Filterer`. This helper interface gives ability to covariantly specify a
69
+
lower bound of contravariant `Predicate` in the subinterfaces of interfaces representing the
70
+
container-like objects.
71
+
72
+
In our example we will be able to pass a predicate that takes `? extends Threat` object and
73
+
return `? extends ThreatAwareSystem` from `Filtered::by` method. A simple implementation
Pattern can be used when working with container-like objects that use subtyping, instead of parametrizing(generics) for extensible class structure.
186
-
It enables you to easily extend filtering ability of container-like objects as business requirements change.
212
+
213
+
Pattern can be used when working with container-like objects that use subtyping, instead of
214
+
parametrizing (generics) for extensible class structure. It enables you to easily extend filtering
215
+
ability of container-like objects as business requirements change.
187
216
188
217
## Tutorials
218
+
189
219
*[Article about Filterer pattern posted on it's author's blog](https://blog.tlinkowski.pl/2018/filterer-pattern/)
190
220
*[Application of Filterer pattern in domain of text analysis](https://www.javacodegeeks.com/2019/02/filterer-pattern-10-steps.html)
191
221
192
222
## Known uses
193
-
One of the uses is present on the blog presented in [this](https://www.javacodegeeks.com/2019/02/filterer-pattern-10-steps.html) link.
194
-
It presents how to use `Filterer` pattern to create text issue analyzer with support for test cases used for unit testing.
223
+
224
+
One of the uses is present on the blog presented in
225
+
[this](https://www.javacodegeeks.com/2019/02/filterer-pattern-10-steps.html) link. It presents how
226
+
to use `Filterer` pattern to create text issue analyzer with support for test cases used for unit
227
+
testing.
195
228
196
229
## Consequences
197
-
Pros :
198
-
* you can easily introduce new subtypes for container-like objects and subtypes for objects that are contained within them and still be able to filter easily be new properties of those new subtypes.
199
230
200
-
Cons :
201
-
* covariant return types mixed with generics can be sometimes tricky
231
+
Pros:
232
+
* You can easily introduce new subtypes for container-like objects and subtypes for objects that are contained within them and still be able to filter easily be new properties of those new subtypes.
233
+
234
+
Cons:
235
+
* Covariant return types mixed with generics can be sometimes tricky
202
236
203
237
## Credits
204
-
* Author of the pattern : [Tomasz Linkowski](https://tlinkowski.pl/)
238
+
239
+
* Author of the pattern : [Tomasz Linkowski](https://tlinkowski.pl/)
0 commit comments