Skip to content

Commit 0b17abd

Browse files
committed
iluwatar#590 Add explanation for Specification pattern
1 parent a8c7771 commit 0b17abd

1 file changed

Lines changed: 67 additions & 3 deletions

File tree

‎specification/README.md‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ categories: Behavioral
77
tags:
88
- Java
99
- Difficulty-Beginner
10+
- Searching
1011
---
1112

1213
## Also known as
@@ -16,15 +17,78 @@ Filter, Criteria
1617
Specification pattern separates the statement of how to match a
1718
candidate, from the candidate object that it is matched against. As well as its
1819
usefulness in selection, it is also valuable for validation and for building to
19-
order
20+
order.
2021

2122
![alt text](./etc/specification.png "Specification")
2223

2324
## Applicability
2425
Use the Specification pattern when
2526

26-
* you need to select a subset of objects based on some criteria, and to refresh the selection at various times
27-
* you need to check that only suitable objects are used for a certain role (validation)
27+
* You need to select a subset of objects based on some criteria, and to refresh the selection at various times.
28+
* You need to check that only suitable objects are used for a certain role (validation).
29+
30+
## Explanation
31+
32+
Real world example
33+
34+
> There is a pool of different creatures and we often need to select some subset of them. We can write our search specification such as "creatures that can fly" and give it to the party that will perform the filtering.
35+
36+
In Plain Words
37+
38+
> Specification pattern allows us to separate the search criteria from the object that performs the search.
39+
40+
Wikipedia says
41+
42+
> In computer programming, the specification pattern is a particular software design pattern, whereby business rules can be recombined by chaining the business rules together using boolean logic.
43+
44+
**Programmatic Example**
45+
46+
If we look at our creature pool example from above, we have a set of creatures with certain properties.
47+
48+
```java
49+
public interface Creature {
50+
String getName();
51+
Size getSize();
52+
Movement getMovement();
53+
Color getColor();
54+
}
55+
```
56+
57+
And dragon implementation looks like this.
58+
59+
```java
60+
public class Dragon extends AbstractCreature {
61+
62+
public Dragon() {
63+
super("Dragon", Size.LARGE, Movement.FLYING, Color.RED);
64+
}
65+
}
66+
```
67+
68+
Now that we want to select some subset of them, we use selectors. To select creatures that fly, we should use MovementSelector.
69+
70+
```java
71+
public class MovementSelector implements Predicate<Creature> {
72+
73+
private final Movement movement;
74+
75+
public MovementSelector(Movement m) {
76+
this.movement = m;
77+
}
78+
79+
@Override
80+
public boolean test(Creature t) {
81+
return t.getMovement().equals(movement);
82+
}
83+
}
84+
```
85+
86+
With these building blocks in place, we can perform a search for red and flying creatures like this.
87+
88+
```java
89+
List<Creature> redAndFlyingCreatures = creatures.stream()
90+
.filter(new ColorSelector(Color.RED).and(new MovementSelector(Movement.FLYING))).collect(Collectors.toList());
91+
```
2892

2993
## Related patterns
3094

0 commit comments

Comments
 (0)