Browser docs

Filterer

Name / classification

Filterer

Intent

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.

Explanation

Real world example

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

In plain words

Filterer pattern is a design pattern that helps container-like objects return filtered versions of themselves.

Programmatic Example

To model the threat detection example presented above we introduce Threat and ThreatAwareSystem interfaces.

 1public interface Threat {
 2  String name();
 3  int id();
 4  ThreatType type();
 5}
 6
 7public interface ThreatAwareSystem {
 8  String systemId();
 9  List<? extends Threat> threats();
10  Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered();
11
12}

Notice the filtered method that returns instance of Filterer interface which is defined as:

1@FunctionalInterface
2public interface Filterer<G, E> {
3  G by(Predicate<? super E> predicate);
4}

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.

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:

 1public class SimpleThreatAwareSystem implements ThreatAwareSystem {
 2
 3  private final String systemId;
 4  private final ImmutableList<Threat> issues;
 5
 6  public SimpleThreatAwareSystem(final String systemId, final List<Threat> issues) {
 7    this.systemId = systemId;
 8    this.issues = ImmutableList.copyOf(issues);
 9  }
10  
11  @Override
12  public String systemId() {
13    return systemId;
14  }
15  
16  @Override
17  public List<? extends Threat> threats() {
18    return new ArrayList<>(issues);
19  }
20
21  @Override
22  public Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered() {
23    return this::filteredGroup;
24  }
25
26  private ThreatAwareSystem filteredGroup(Predicate<? super Threat> predicate) {
27    return new SimpleThreatAwareSystem(this.systemId, filteredItems(predicate));
28  }
29
30  private List<Threat> filteredItems(Predicate<? super Threat> predicate) {
31    return this.issues.stream()
32            .filter(predicate)
33            .collect(Collectors.toList());
34  }
35}

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 threat can appear:

1public interface ProbableThreat extends Threat {
2  double probability();
3}

We can also introduce a new interface that represents a system that is aware of threats with their probabilities:

1public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
2  @Override
3  List<? extends ProbableThreat> threats();
4
5  @Override
6  Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered();
7}

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 we will be able to filter ProbabilisticThreatAwareSystem by ProbableThreat properties:

 1public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreatAwareSystem {
 2
 3  private final String systemId;
 4  private final ImmutableList<ProbableThreat> threats;
 5
 6  public SimpleProbabilisticThreatAwareSystem(final String systemId, final List<ProbableThreat> threats) {
 7    this.systemId = systemId;
 8    this.threats = ImmutableList.copyOf(threats);
 9  }
10
11  @Override
12  public String systemId() {
13    return systemId;
14  }
15
16  @Override
17  public List<? extends ProbableThreat> threats() {
18    return threats;
19  }
20
21  @Override
22  public Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered() {
23    return this::filteredGroup;
24  }
25
26  private ProbabilisticThreatAwareSystem filteredGroup(final Predicate<? super ProbableThreat> predicate) {
27    return new SimpleProbabilisticThreatAwareSystem(this.systemId, filteredItems(predicate));
28  }
29
30  private List<ProbableThreat> filteredItems(final Predicate<? super ProbableThreat> predicate) {
31    return this.threats.stream()
32            .filter(predicate)
33            .collect(Collectors.toList());
34  }
35}

Now if we want filter ThreatAwareSystem by threat type we can do:

1Threat rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit");
2Threat trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan");
3List<Threat> threats = List.of(rootkit, trojan);
4
5ThreatAwareSystem threatAwareSystem = new SimpleThreatAwareSystem("System-1", threats);
6
7ThreatAwareSystem rootkitThreatAwareSystem = threatAwareSystem.filtered()
8           .by(threat -> threat.type() == ThreatType.ROOTKIT);

Or if we want to filter ProbabilisticThreatAwareSystem:

1ProbableThreat malwareTroyan = new SimpleProbableThreat("Troyan-ArcBomb", 1, ThreatType.TROJAN, 0.99);
2ProbableThreat rootkit = new SimpleProbableThreat("Rootkit-System", 2, ThreatType.ROOTKIT, 0.8);
3List<ProbableThreat> probableThreats = List.of(malwareTroyan, rootkit);
4
5ProbabilisticThreatAwareSystem simpleProbabilisticThreatAwareSystem =new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats);
6
7ProbabilisticThreatAwareSystem filtered = simpleProbabilisticThreatAwareSystem.filtered()
8           .by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);

Class diagram

Filterer

Applicability

Pattern can be used when working with container-like objects that use subtyping, instead of parametrizing (generics) for extensible class structure. It enables you to easily extend filtering ability of container-like objects as business requirements change.

Tutorials

Known uses

One of the uses is present on the blog presented in this link. It presents how to use Filterer pattern to create text issue analyzer with support for test cases used for unit testing.

Consequences

Pros:

  • 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:

  • Covariant return types mixed with generics can be sometimes tricky

Credits