|
9 | 9 | ---
|
10 | 10 |
|
11 | 11 | ## Intent
|
12 |
| -A fluent interface provides an easy-readable, flowing interface, that often mimics a domain specific language. Using |
13 |
| -this pattern results in code that can be read nearly as human language. |
| 12 | + |
| 13 | +A fluent interface provides an easy-readable, flowing interface, that often mimics a domain specific |
| 14 | +language. Using this pattern results in code that can be read nearly as human language. |
14 | 15 |
|
15 | 16 | ## Explanation
|
16 | 17 |
|
17 |
| -The Fluent Interface pattern is useful when you want to provide an easy readable, flowing API. Those interfaces tend |
18 |
| -to mimic domain specific languages, so they can nearly be read as human languages. |
| 18 | +The Fluent Interface pattern is useful when you want to provide an easy readable, flowing API. Those |
| 19 | +interfaces tend to mimic domain specific languages, so they can nearly be read as human languages. |
19 | 20 |
|
20 | 21 | A fluent interface can be implemented using any of
|
21 | 22 |
|
22 |
| - * Method Chaining - calling a method returns some object on which further methods can be called. |
23 |
| - * Static Factory Methods and Imports |
| 23 | + * Method chaining - calling a method returns some object on which further methods can be called. |
| 24 | + * Static factory methods and imports. |
24 | 25 | * Named parameters - can be simulated in Java using static factory methods.
|
25 | 26 |
|
26 | 27 | Real world example
|
27 | 28 |
|
28 |
| -> We need to select numbers based on different criteria from the list. It's a great chance to utilize fluent interface pattern to provide readable easy-to-use developer experience. |
| 29 | +> We need to select numbers based on different criteria from the list. It's a great chance to |
| 30 | +> utilize fluent interface pattern to provide readable easy-to-use developer experience. |
29 | 31 |
|
30 | 32 | In plain words
|
31 | 33 |
|
32 | 34 | > Fluent Interface pattern provides easily readable flowing interface to code.
|
33 | 35 |
|
34 | 36 | Wikipedia says
|
35 | 37 |
|
36 |
| -> In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). |
| 38 | +> In software engineering, a fluent interface is an object-oriented API whose design relies |
| 39 | +> extensively on method chaining. Its goal is to increase code legibility by creating a |
| 40 | +> domain-specific language (DSL). |
37 | 41 |
|
38 | 42 | **Programmatic Example**
|
39 | 43 |
|
@@ -134,29 +138,35 @@ result is printed afterwards.
|
134 | 138 | .first(2)
|
135 | 139 | .last()
|
136 | 140 | .ifPresent(number -> LOGGER.info("Last amongst first two negatives: {}", number));
|
137 |
| - |
138 |
| - // The initial list contains: 1, -61, 14, -22, 18, -87, 6, 64, -82, 26, -98, 97, 45, 23, 2, -68. |
139 |
| - // The first three negative values are: -61, -22, -87. |
140 |
| - // The last two positive values are: 23, 2. |
141 |
| - // The first even number is: 14 |
142 |
| - // A string-mapped list of negative numbers contains: String[-61], String[-22], String[-87], String[-82], String[-98], String[-68]. |
143 |
| - // The lazy list contains the last two of the first four positive numbers mapped to Strings: String[18], String[6]. |
144 |
| - // Last amongst first two negatives: -22 |
| 141 | +``` |
| 142 | + |
| 143 | +Program output: |
| 144 | + |
| 145 | +```java |
| 146 | +The initial list contains: 1, -61, 14, -22, 18, -87, 6, 64, -82, 26, -98, 97, 45, 23, 2, -68. |
| 147 | +The first three negative values are: -61, -22, -87. |
| 148 | +The last two positive values are: 23, 2. |
| 149 | +The first even number is: 14 |
| 150 | +A string-mapped list of negative numbers contains: String[-61], String[-22], String[-87], String[-82], String[-98], String[-68]. |
| 151 | +The lazy list contains the last two of the first four positive numbers mapped to Strings: String[18], String[6]. |
| 152 | +Last amongst first two negatives: -22 |
145 | 153 | ```
|
146 | 154 |
|
147 | 155 | ## Class diagram
|
| 156 | + |
148 | 157 | 
|
149 | 158 |
|
150 | 159 | ## Applicability
|
| 160 | + |
151 | 161 | Use the Fluent Interface pattern when
|
152 | 162 |
|
153 |
| -* You provide an API that would benefit from a DSL-like usage |
154 |
| -* You have objects that are difficult to configure or use |
| 163 | +* You provide an API that would benefit from a DSL-like usage. |
| 164 | +* You have objects that are difficult to configure or use. |
155 | 165 |
|
156 | 166 | ## Known uses
|
157 | 167 |
|
158 | 168 | * [Java 8 Stream API](http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html)
|
159 |
| -* [Google Guava FluentInterable](https://github.com/google/guava/wiki/FunctionalExplained) |
| 169 | +* [Google Guava FluentIterable](https://github.com/google/guava/wiki/FunctionalExplained) |
160 | 170 | * [JOOQ](http://www.jooq.org/doc/3.0/manual/getting-started/use-cases/jooq-as-a-standalone-sql-builder/)
|
161 | 171 | * [Mockito](http://mockito.org/)
|
162 | 172 | * [Java Hamcrest](http://code.google.com/p/hamcrest/wiki/Tutorial)
|
|
0 commit comments