Skip to content

Commit 015b418

Browse files
committed
Update README.md
1 parent 0cba307 commit 015b418

1 file changed

Lines changed: 66 additions & 31 deletions

File tree

‎filterer/README.md‎

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,33 @@ tags:
1111
---
1212

1313
## Name / classification
14+
1415
Filterer
1516

1617
## 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.
1821

1922
## Explanation
23+
2024
Real world example
2125

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).
2631
2732
In plain words
2833

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.
3136
3237
**Programmatic Example**
3338

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.
3541

3642
```java
3743
public interface Threat {
@@ -47,19 +53,26 @@ public interface ThreatAwareSystem {
4753

4854
}
4955
```
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+
5159
```java
5260
@FunctionalInterface
5361
public interface Filterer<G, E> {
5462
G by(Predicate<? super E> predicate);
5563
}
5664
```
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.
6065

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
74+
of `ThreatAwareSystem`:
75+
6376
```java
6477
public class SimpleThreatAwareSystem implements ThreatAwareSystem {
6578

@@ -97,15 +110,21 @@ public class SimpleThreatAwareSystem implements ThreatAwareSystem {
97110
}
98111
}
99112
```
100-
the `filtered` method is overridden to filter the threats list by given predicate.
101113

102-
Now if we introduce a new subtype of `Threat` interface that adds probability with which given threat can appear :
114+
The `filtered` method is overridden to filter the threats list by given predicate.
115+
116+
Now if we introduce a new subtype of `Threat` interface that adds probability with which given
117+
threat can appear:
118+
103119
```java
104120
public interface ProbableThreat extends Threat {
105121
double probability();
106122
}
107123
```
108-
we can also introduce a new interface that represents a system that is aware of threats with their probabilities :
124+
125+
We can also introduce a new interface that represents a system that is aware of threats with their
126+
probabilities:
127+
109128
````java
110129
public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
111130
@Override
@@ -115,9 +134,12 @@ public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
115134
Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered();
116135
}
117136
````
118-
Notice how we override the `filtered` method in `ProbabilisticThreatAwareSystem` and specify different return covariant type
119-
by specifying different generic types. Our interfaces are clean and not cluttered by default implementations. We
120-
we will be able to filter `ProbabilisticThreatAwareSystem` by `ProbableThreat` properties :
137+
138+
Notice how we override the `filtered` method in `ProbabilisticThreatAwareSystem` and specify
139+
different return covariant type by specifying different generic types. Our interfaces are clean and
140+
not cluttered by default implementations. We we will be able to filter
141+
`ProbabilisticThreatAwareSystem` by `ProbableThreat` properties:
142+
121143
```java
122144
public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreatAwareSystem {
123145

@@ -156,7 +178,8 @@ public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreat
156178
}
157179
```
158180

159-
Now if we want filter `ThreatAwareSystem` by threat type we can do :
181+
Now if we want filter `ThreatAwareSystem` by threat type we can do:
182+
160183
```java
161184
Threat rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit");
162185
Threat trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan");
@@ -167,7 +190,9 @@ ThreatAwareSystem threatAwareSystem = new SimpleThreatAwareSystem("System-1", th
167190
ThreatAwareSystem rootkitThreatAwareSystem = threatAwareSystem.filtered()
168191
.by(threat -> threat.type() == ThreatType.ROOTKIT);
169192
```
170-
or if we want to filter `ProbabilisticThreatAwareSystem` :
193+
194+
Or if we want to filter `ProbabilisticThreatAwareSystem`:
195+
171196
```java
172197
ProbableThreat malwareTroyan = new SimpleProbableThreat("Troyan-ArcBomb", 1, ThreatType.TROJAN, 0.99);
173198
ProbableThreat rootkit = new SimpleProbableThreat("Rootkit-System", 2, ThreatType.ROOTKIT, 0.8);
@@ -178,27 +203,37 @@ ProbabilisticThreatAwareSystem simpleProbabilisticThreatAwareSystem =new SimpleP
178203
ProbabilisticThreatAwareSystem filtered = simpleProbabilisticThreatAwareSystem.filtered()
179204
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
180205
```
206+
181207
## Class diagram
208+
182209
![Filterer](./etc/filterer.png "Filterer")
183210

184211
## Applicability
185-
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.
187216

188217
## Tutorials
218+
189219
* [Article about Filterer pattern posted on it's author's blog](https://blog.tlinkowski.pl/2018/filterer-pattern/)
190220
* [Application of Filterer pattern in domain of text analysis](https://www.javacodegeeks.com/2019/02/filterer-pattern-10-steps.html)
191221

192222
## 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.
195228

196229
## 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.
199230

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
202236

203237
## 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

Comments
 (0)