Skip to content

Commit 8037495

Browse files
committed
Add some formatting to Specification pattern readme
1 parent 6941e65 commit 8037495

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

specification/README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ Wikipedia says
4444
4545
**Programmatic Example**
4646

47-
If we look at our creature pool example from above, we have a set of creatures with certain properties.\
47+
If we look at our creature pool example from above, we have a set of creatures with certain properties.
4848
Those properties can be part of a pre-defined, limited set (represented here by the enums Size, Movement and Color); but they can also be continuous values (e.g. the mass of a Creature).
4949
In this case, it is more appropriate to use what we call "parameterized specification", where the property value can be given as an argument when the Creature is instantiated, allowing for more flexibility.
50-
A third option is to combine pre-defined and/or parameterized properties using boolean logic, allowing for near-endless selection possibilities (this is called Composite Specification, see below).
50+
A third option is to combine pre-defined and/or parameterized properties using boolean logic, allowing for near-endless selection possibilities (this is called "composite specification", see below).
5151
The pros and cons of each approach are detailed in the table at the end of this document.
52+
5253
```java
5354
public interface Creature {
5455
String getName();
@@ -60,6 +61,7 @@ public interface Creature {
6061
```
6162

6263
And ``Dragon`` implementation looks like this.
64+
6365
```java
6466
public class Dragon extends AbstractCreature {
6567

@@ -70,6 +72,7 @@ public class Dragon extends AbstractCreature {
7072
```
7173

7274
Now that we want to select some subset of them, we use selectors. To select creatures that fly, we should use ``MovementSelector``.
75+
7376
```java
7477
public class MovementSelector extends AbstractSelector<Creature> {
7578

@@ -87,6 +90,7 @@ public class MovementSelector extends AbstractSelector<Creature> {
8790
```
8891

8992
On the other hand, when selecting creatures heavier than a chosen amount, we use ``MassGreaterThanSelector``.
93+
9094
```java
9195
public class MassGreaterThanSelector extends AbstractSelector<Creature> {
9296

@@ -103,19 +107,22 @@ public class MassGreaterThanSelector extends AbstractSelector<Creature> {
103107
}
104108
```
105109

106-
With these building blocks in place, we can perform a search for red creatures as follows :
110+
With these building blocks in place, we can perform a search for red creatures as follows:
111+
107112
```java
108113
List<Creature> redCreatures = creatures.stream().filter(new ColorSelector(Color.RED))
109114
.collect(Collectors.toList());
110115
```
111116

112-
But we could also use our parameterized selector like this :
117+
But we could also use our parameterized selector like this:
118+
113119
```java
114120
List<Creature> heavyCreatures = creatures.stream().filter(new MassGreaterThanSelector(500.0)
115121
.collect(Collectors.toList());
116122
```
117123

118-
Our third option is to combine multiple selectors together. Performing a search for special creatures (defined as red, flying, and not small) could be done as follows :
124+
Our third option is to combine multiple selectors together. Performing a search for special creatures (defined as red, flying, and not small) could be done as follows:
125+
119126
```java
120127
AbstractSelector specialCreaturesSelector =
121128
new ColorSelector(Color.RED).and(new MovementSelector(Movement.FLYING)).and(new SizeSelector(Size.SMALL).not());
@@ -128,6 +135,7 @@ Our third option is to combine multiple selectors together. Performing a search
128135

129136
In Composite Specification, we will create custom instances of ``AbstractSelector`` by combining other selectors (called "leaves") using the three basic logical operators.
130137
These are implemented in ``ConjunctionSelector``, ``DisjunctionSelector`` and ``NegationSelector``.
138+
131139
```java
132140
public abstract class AbstractSelector<T> implements Predicate<T> {
133141

@@ -144,6 +152,7 @@ public abstract class AbstractSelector<T> implements Predicate<T> {
144152
}
145153
}
146154
```
155+
147156
```java
148157
public class ConjunctionSelector<T> extends AbstractSelector<T> {
149158

0 commit comments

Comments
 (0)