Skip to content

Commit 6894862

Browse files
committed
iluwatar#590 add explanation to Pipeline
1 parent 9deb587 commit 6894862

File tree

2 files changed

+84
-7
lines changed

2 files changed

+84
-7
lines changed

pipeline/README.md

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,83 @@ tags:
99
---
1010

1111
## Intent
12-
Allows processing of data in a series of stages by giving in an initial input and passing the processed output to be used by the next stages.
12+
Allows processing of data in a series of stages by giving in an initial input and passing the processed output to be
13+
used by the next stages.
14+
15+
## Explanation
16+
17+
The Pipeline pattern uses ordered stages to process a sequence of input values. Each implemented task is represented by
18+
a stage of the pipeline. You can think of pipelines as similar to assembly lines in a factory, where each item in the
19+
assembly line is constructed in stages. The partially assembled item is passed from one assembly stage to another. The
20+
outputs of the assembly line occur in the same order as that of the inputs.
21+
22+
Real world example
23+
24+
> Suppose we wanted to pass through a string to a series of filtering stages and convert it as a char array on the last stage.
25+
26+
In plain words
27+
28+
> Pipeline pattern is an assembly line where partial results are passed from one stage to another.
29+
30+
Wikipedia says
31+
32+
> In software engineering, a pipeline consists of a chain of processing elements (processes, threads, coroutines, functions, etc.), arranged so that the output of each element is the input of the next; the name is by analogy to a physical pipeline.
33+
34+
**Programmatic Example**
35+
36+
The stages of our pipeline are called `Handler`s.
37+
38+
```java
39+
interface Handler<I, O> {
40+
O process(I input);
41+
}
42+
```
43+
44+
In our string processing example we have 3 different concrete `Handler`s.
45+
46+
```java
47+
class RemoveAlphabetsHandler implements Handler<String, String> {
48+
...
49+
}
50+
51+
class RemoveDigitsHandler implements Handler<String, String> {
52+
...
53+
}
54+
55+
class ConvertToCharArrayHandler implements Handler<String, char[]> {
56+
...
57+
}
58+
```
59+
60+
Here is the `Pipeline` that will gather and execute the handlers one by one.
61+
62+
```java
63+
class Pipeline<I, O> {
64+
65+
private final Handler<I, O> currentHandler;
66+
67+
Pipeline(Handler<I, O> currentHandler) {
68+
this.currentHandler = currentHandler;
69+
}
70+
71+
<K> Pipeline<I, K> addHandler(Handler<O, K> newHandler) {
72+
return new Pipeline<>(input -> newHandler.process(currentHandler.process(input)));
73+
}
74+
75+
O execute(I input) {
76+
return currentHandler.process(input);
77+
}
78+
}
79+
```
80+
81+
And here's the `Pipeline` in action processing the string.
82+
83+
```java
84+
var filters = new Pipeline<>(new RemoveAlphabetsHandler())
85+
.addHandler(new RemoveDigitsHandler())
86+
.addHandler(new ConvertToCharArrayHandler());
87+
filters.execute("GoYankees123!");
88+
```
1389

1490
## Class diagram
1591
![alt text](./etc/pipeline.urm.png "Pipeline pattern class diagram")
@@ -21,16 +97,16 @@ Use the Pipeline pattern when you want to
2197
* Add readability to complex sequence of operations by providing a fluent builder as an interface
2298
* Improve testability of code since stages will most likely be doing a single thing, complying to the [Single Responsibility Principle (SRP)](https://java-design-patterns.com/principles/#single-responsibility-principle)
2399

24-
## Typical Use Case
25-
26-
* Implement stages and execute them in an ordered manner
27-
28-
## Real world examples
100+
## Known uses
29101

30102
* [java.util.Stream](https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html)
31103
* [Maven Build Lifecycle](http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html)
32104
* [Functional Java](https://github.com/functionaljava/functionaljava)
33105

106+
## Related patterns
107+
108+
* [Chain of Responsibility](https://java-design-patterns.com/patterns/chain/)
109+
34110
## Credits
35111

36112
* [The Pipeline Pattern — for fun and profit](https://medium.com/@aaronweatherall/the-pipeline-pattern-for-fun-and-profit-9b5f43a98130)

pipeline/src/main/java/com/iluwatar/pipeline/App.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ public static void main(String[] args) {
5959
then is expected to receive an input of char[] array since that is the type being returned
6060
by the previous handler, ConvertToCharArrayHandler.
6161
*/
62-
new Pipeline<>(new RemoveAlphabetsHandler())
62+
var filters = new Pipeline<>(new RemoveAlphabetsHandler())
6363
.addHandler(new RemoveDigitsHandler())
6464
.addHandler(new ConvertToCharArrayHandler());
65+
filters.execute("GoYankees123!");
6566
}
6667
}

0 commit comments

Comments
 (0)