You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To enable creating complex functions by composing simpler ones, enhancing modularity and reusability of function-based logic.
17
23
18
24
## Explanation
19
25
20
-
Real-world example:
26
+
Real-world example
21
27
22
-
> In financial software, functions that calculate various financial metrics can be composed to provide detailed analysis. For instance, a function that calculates interest can be composed with another that adjusts for taxes, allowing for a modular yet comprehensive financial assessment tool.
28
+
> Imagine a fast-food restaurant where the process of making a burger is broken down into several steps: grilling the patty, toasting the bun, adding condiments, and assembling the burger. Each of these steps can be seen as a function.
29
+
>
30
+
> In the Functional Composition design pattern, these individual steps (functions) can be composed into a complete burger-making process. Each step remains simple and reusable. For instance, the grilling function could be reused for making sandwiches or other dishes that require a grilled patty. This modular approach allows the restaurant to efficiently create various menu items by reusing and combining simple, predefined steps.
23
31
24
-
In plain words:
32
+
In plain words
25
33
26
34
> The Function Composer pattern allows building complex functions by combining simpler ones, making it easier to manage, test, and reuse individual pieces of functionality.
27
35
28
-
Wikipedia says:
36
+
Wikipedia says
29
37
30
38
> Function composition is an act or mechanism to combine simple functions to build more complicated ones. Like the usual composition of functions in mathematics, the result of each function is passed as the argument of the next, and the result of the last one is the result of the whole.
31
39
32
40
**Programmatic Example**
33
41
34
-
Here is how the Function Composer pattern might be implemented and used in Java:
42
+
Let's start with defining two simple functions. In this case, we have a function `timesTwo` that multiplies its input by 2, and a function `square` that squares its input.
Next, we use the `FunctionComposer` class to compose these two functions into a new function. The `composeFunctions` method takes two functions as arguments and returns a new function that is the composition of the input functions.
Finally, we apply the composed function to an input value. In this case, we apply it to the number 3. The result is the square of the number 3 multiplied by 2, which is 36.
logger.info("Result of composing 'timesTwo' and 'square' functions applied to 3 is: "+ result);
54
-
}
65
+
int result = composedFunction.apply(3);
66
+
logger.info("Result of composing 'timesTwo' and 'square' functions applied to 3 is: "+ result);
55
67
}
56
68
```
57
69
58
-
Result:
70
+
This will output:
71
+
59
72
```
60
-
Result of composing 'timesTwo' and 'square' functions applied to 3 is: 36 // Result will be 36 (3 * 2 = 6, 6 * 6 = 36)
73
+
Result of composing 'timesTwo' and 'square' functions applied to 3 is: 36
61
74
```
62
75
63
-
Use ``.compose()`` function when you need pre-compose and ``.andThen()`` function when you need post-compose.
76
+
This example demonstrates how the Function Composition pattern can be used to create complex functions by composing simpler ones, enhancing modularity and reusability of function-based logic.
@@ -73,10 +86,13 @@ Use the Function Composer pattern when:
73
86
* You want to create a pipeline of operations where the output of one function is the input to another.
74
87
* You need to enhance the clarity and quality of your code by structuring complex function logic into simpler, reusable components.
75
88
* You are working in a functional programming environment or a language that supports higher-order functions.
89
+
* When you want to avoid deep nesting of function calls and instead build a pipeline of operations.
90
+
* When aiming to promote immutability and side-effect-free functions in your design.
76
91
77
92
## Tutorials
78
93
79
-
[Function Composition in Java](https://functionalprogramming.medium.com/function-composition-in-java-beaf39426f52)
94
+
*[Function Composition in Java (Medium)](https://functionalprogramming.medium.com/function-composition-in-java-beaf39426f52)
95
+
*[Functional Programming in Java (Baeldung)](https://www.baeldung.com/java-functional-programming)
80
96
81
97
## Known uses
82
98
@@ -91,19 +107,23 @@ Benefits:
91
107
* High reusability of composed functions.
92
108
* Increased modularity, making complex functions easier to understand and maintain.
93
109
* Flexible and dynamic creation of function pipelines at runtime.
94
-
*
95
-
Drawbacks:
110
+
* Enhances readability by structuring code in a linear, declarative manner.
111
+
* Facilitates easier testing of individual functions.
112
+
113
+
Trade-offs:
96
114
97
115
* Potentially higher complexity when debugging composed functions.
98
116
* Overhead from creating and managing multiple function objects in memory-intensive scenarios.
117
+
* May require a paradigm shift for developers unfamiliar with functional programming concepts.
99
118
100
119
## Related patterns
101
120
102
-
* Chain of Responsibility
103
-
* Decorator
104
-
* Strategy
121
+
*[Chain of Responsibility](https://java-design-patterns.com/patterns/chain-of-responsibility/) - Both patterns allow processing to be broken down into a series of steps, but Functional Composition focuses on function composition rather than responsibility delegation.
122
+
*[Decorator](https://java-design-patterns.com/patterns/decorator/) - Similar in combining behaviors, but Decorator applies additional behavior to objects, while Functional Composition builds new functions.
123
+
*[Strategy](https://java-design-patterns.com/patterns/strategy/) - Provides interchangeable functions (strategies), which can be composed in Functional Composition.
105
124
106
125
## Credits
107
126
108
-
[Functional Programming in Java](https://www.baeldung.com/java-functional-programming)
109
-
[Function Composition in Java](https://functionalprogramming.medium.com/function-composition-in-java-beaf39426f52)
127
+
*[Effective Java](https://amzn.to/4cGk2Jz)
128
+
*[Functional Programming in Java](https://amzn.to/3JUIc5Q)
129
+
*[Java 8 in Action: Lambdas, Streams, and functional-style programming](https://amzn.to/3QCmGXs)
0 commit comments