mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-17 02:14:16 +00:00
docs: update filterer
This commit is contained in:
1 parent
6a97047aaa
commit
6d385349cb
6 files changed
+53
-58
No files matched your search
+48
-51
@@ -1,73 +1,69 @@
|
|||||||
---
|
---
|
||||||
title: Filterer
|
title: Filterer
|
||||||
language: en
|
language: en
|
||||||
category: Functional
|
category: Behavioral
|
||||||
tag:
|
tag:
|
||||||
- Extensibility
|
- Data processing
|
||||||
|
- Data transformation
|
||||||
|
- Decoupling
|
||||||
|
- Performance
|
||||||
|
- Runtime
|
||||||
---
|
---
|
||||||
|
|
||||||
## Name / classification
|
## Also known as
|
||||||
|
|
||||||
Filterer
|
* Filters
|
||||||
|
* Pipes and Filters
|
||||||
|
|
||||||
## Intent
|
## Intent
|
||||||
|
|
||||||
The intent of this design pattern is to introduce a functional interface that will add a
|
The Filterer pattern aims to apply a series of filters to data objects, where each filter processes the data based on specific rules and criteria, and passes the data to the next filter in the sequence.
|
||||||
functionality for container-like objects to easily return filtered versions of themselves.
|
|
||||||
|
|
||||||
## Explanation
|
## Explanation
|
||||||
|
|
||||||
Real world example
|
Real world example
|
||||||
|
|
||||||
> We are designing a threat (malware) detection software which can analyze target systems for
|
> 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).
|
||||||
> 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).
|
|
||||||
|
|
||||||
In plain words
|
In plain words
|
||||||
|
|
||||||
> Filterer pattern is a design pattern that helps container-like objects return filtered versions
|
> Filterer pattern is a design pattern that helps container-like objects return filtered versions of themselves.
|
||||||
> of themselves.
|
|
||||||
|
|
||||||
**Programmatic Example**
|
**Programmatic Example**
|
||||||
|
|
||||||
To model the threat detection example presented above we introduce `Threat` and `ThreatAwareSystem`
|
To model the threat detection example presented above we introduce `Threat` and `ThreatAwareSystem` interfaces.
|
||||||
interfaces.
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public interface Threat {
|
public interface Threat {
|
||||||
String name();
|
String name();
|
||||||
|
|
||||||
int id();
|
int id();
|
||||||
|
|
||||||
ThreatType type();
|
ThreatType type();
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface ThreatAwareSystem {
|
public interface ThreatAwareSystem {
|
||||||
String systemId();
|
String systemId();
|
||||||
List<? extends Threat> threats();
|
|
||||||
Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered();
|
|
||||||
|
|
||||||
|
List<? extends Threat> threats();
|
||||||
|
|
||||||
|
Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Notice the `filtered` method that returns instance of `Filterer` interface which is defined as:
|
Notice the `filtered` method that returns instance of `Filterer` interface which is defined as:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface Filterer<G, E> {
|
public interface Filterer<G, E> {
|
||||||
G by(Predicate<? super E> predicate);
|
G by(Predicate<? super E> predicate);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
It is used to fulfill the requirement for system to be able to filter itself based on threat
|
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.
|
||||||
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.
|
|
||||||
|
|
||||||
In our example we will be able to pass a predicate that takes `? extends Threat` object and
|
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`:
|
||||||
return `? extends ThreatAwareSystem` from `Filtered::by` method. A simple implementation
|
|
||||||
of `ThreatAwareSystem`:
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class SimpleThreatAwareSystem implements ThreatAwareSystem {
|
public class SimpleThreatAwareSystem implements ThreatAwareSystem {
|
||||||
@@ -109,8 +105,7 @@ public class SimpleThreatAwareSystem implements ThreatAwareSystem {
|
|||||||
|
|
||||||
The `filtered` method is overridden to filter the threats list by given predicate.
|
The `filtered` method is overridden to filter the threats list by given predicate.
|
||||||
|
|
||||||
Now if we introduce a new subtype of `Threat` interface that adds probability with which given
|
Now if we introduce a new subtype of `Threat` interface that adds probability with which given threat can appear:
|
||||||
threat can appear:
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public interface ProbableThreat extends Threat {
|
public interface ProbableThreat extends Threat {
|
||||||
@@ -118,8 +113,7 @@ public interface ProbableThreat extends Threat {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
We can also introduce a new interface that represents a system that is aware of threats with their
|
We can also introduce a new interface that represents a system that is aware of threats with their probabilities:
|
||||||
probabilities:
|
|
||||||
|
|
||||||
````java
|
````java
|
||||||
public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
|
public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
|
||||||
@@ -131,10 +125,7 @@ public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
|
|||||||
}
|
}
|
||||||
````
|
````
|
||||||
|
|
||||||
Notice how we override the `filtered` method in `ProbabilisticThreatAwareSystem` and specify
|
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:
|
||||||
different return covariant type by specifying different generic types. Our interfaces are clean and
|
|
||||||
not cluttered by default implementations. We we will be able to filter
|
|
||||||
`ProbabilisticThreatAwareSystem` by `ProbableThreat` properties:
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreatAwareSystem {
|
public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreatAwareSystem {
|
||||||
@@ -183,8 +174,7 @@ List<Threat> threats = List.of(rootkit, trojan);
|
|||||||
|
|
||||||
ThreatAwareSystem threatAwareSystem=new SimpleThreatAwareSystem("System-1", threats);
|
ThreatAwareSystem threatAwareSystem=new SimpleThreatAwareSystem("System-1", threats);
|
||||||
|
|
||||||
ThreatAwareSystem rootkitThreatAwareSystem = threatAwareSystem.filtered()
|
ThreatAwareSystem rootkitThreatAwareSystem=threatAwareSystem.filtered().by(threat -> threat.type() == ThreatType.ROOTKIT);
|
||||||
.by(threat -> threat.type() == ThreatType.ROOTKIT);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Or if we want to filter `ProbabilisticThreatAwareSystem`:
|
Or if we want to filter `ProbabilisticThreatAwareSystem`:
|
||||||
@@ -196,8 +186,7 @@ List<ProbableThreat> probableThreats = List.of(malwareTroyan, rootkit);
|
|||||||
|
|
||||||
ProbabilisticThreatAwareSystem simpleProbabilisticThreatAwareSystem = new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats);
|
ProbabilisticThreatAwareSystem simpleProbabilisticThreatAwareSystem = new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats);
|
||||||
|
|
||||||
ProbabilisticThreatAwareSystem filtered = simpleProbabilisticThreatAwareSystem.filtered()
|
ProbabilisticThreatAwareSystem filtered = simpleProbabilisticThreatAwareSystem.filtered().by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
|
||||||
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Class diagram
|
## Class diagram
|
||||||
@@ -206,30 +195,38 @@ ProbabilisticThreatAwareSystem filtered = simpleProbabilisticThreatAwareSystem.f
|
|||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
|
|
||||||
Pattern can be used when working with container-like objects that use subtyping, instead of
|
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.
|
||||||
parametrizing (generics) for extensible class structure. It enables you to easily extend filtering
|
|
||||||
ability of container-like objects as business requirements change.
|
|
||||||
|
|
||||||
## Tutorials
|
## Tutorials
|
||||||
|
|
||||||
* [Article about Filterer pattern posted on it's author's blog](https://blog.tlinkowski.pl/2018/filterer-pattern/)
|
* [Article about Filterer pattern posted on its author's blog](https://blog.tlinkowski.pl/2018/filterer-pattern/)
|
||||||
* [Application of Filterer pattern in domain of text analysis](https://www.javacodegeeks.com/2019/02/filterer-pattern-10-steps.html)
|
* [Application of Filterer pattern in domain of text analysis](https://www.javacodegeeks.com/2019/02/filterer-pattern-10-steps.html)
|
||||||
|
|
||||||
## Known uses
|
## Known Uses
|
||||||
|
|
||||||
One of the uses is present on the blog presented in
|
* Stream processing libraries in Java, such as Apache Kafka Streams, utilize this pattern to build complex data processing pipelines.
|
||||||
[this](https://www.javacodegeeks.com/2019/02/filterer-pattern-10-steps.html) link. It presents how
|
* Image processing software often uses filters to apply effects or transformations to images sequentially.
|
||||||
to use `Filterer` pattern to create text issue analyzer with support for test cases used for unit
|
|
||||||
testing.
|
|
||||||
|
|
||||||
## Consequences
|
## Consequences
|
||||||
|
|
||||||
Pros:
|
Benefits:
|
||||||
* 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.
|
|
||||||
|
|
||||||
Cons:
|
* Increases flexibility by allowing different filters to be added or reorganized without affecting other parts of the system.
|
||||||
* Covariant return types mixed with generics can be sometimes tricky
|
* Enhances testability, as filters can be tested independently.
|
||||||
|
* Promotes loose coupling between the stages of data processing.
|
||||||
|
|
||||||
|
## Trade-offs:
|
||||||
|
|
||||||
|
* Potential performance overhead from continuous data passing between filters.
|
||||||
|
* Complexity can increase with the number of filters, potentially affecting maintainability.
|
||||||
|
|
||||||
|
## Related 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.
|
||||||
|
* Decorator: Similar to Decorator in that both modify behavior dynamically; however, filters focus more on data transformation than on adding responsibilities.
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
* Author of the pattern : [Tomasz Linkowski](https://tlinkowski.pl/)
|
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3W8sn2W)
|
||||||
|
* [Kafka: The Definitive Guide: Real-Time Data and Stream Processing at Scale](https://amzn.to/49N3nRU)
|
||||||
|
* [Java Performance: The Definitive Guide](https://amzn.to/3vRW3qj)
|
||||||
@@ -42,7 +42,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
* objects are systems that are aware of threats that they can be vulnerable to. We would like
|
* objects are systems that are aware of threats that they can be vulnerable to. We would like
|
||||||
* to have a way to create copy of different system objects but with filtered threats.
|
* to have a way to create copy of different system objects but with filtered threats.
|
||||||
* The thing is to keep it simple if we add new subtype of {@link Threat}
|
* The thing is to keep it simple if we add new subtype of {@link Threat}
|
||||||
* (for example {@link ProbableThreat}) - we still need to be able to filter by it's properties.
|
* (for example {@link ProbableThreat}) - we still need to be able to filter by its properties.
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class App {
|
public class App {
|
||||||
|
|||||||
+1
-1
@@ -28,7 +28,7 @@ import com.iluwatar.filterer.domain.Filterer;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents system that is aware of it's threats with given probability of their occurrence.
|
* Represents system that is aware of its threats with given probability of their occurrence.
|
||||||
*/
|
*/
|
||||||
public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
|
public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
|
||||||
|
|
||||||
|
|||||||
-1
@@ -27,7 +27,6 @@ package com.iluwatar.filterer.threat;
|
|||||||
import com.iluwatar.filterer.domain.Filterer;
|
import com.iluwatar.filterer.domain.Filterer;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ import com.iluwatar.filterer.domain.Filterer;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|||||||
+3
-3
@@ -24,10 +24,10 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.filterer.threat;
|
package com.iluwatar.filterer.threat;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import java.util.List;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
class SimpleThreatAwareSystemTest {
|
class SimpleThreatAwareSystemTest {
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in new issue
Block a user