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
Copy file name to clipboardExpand all lines: function-composition/README.md
+22-24
Original file line number
Diff line number
Diff line change
@@ -5,18 +5,20 @@ description: "Learn about the Function Composition design pattern in Java. Disco
5
5
category: Functional
6
6
language: en
7
7
tag:
8
+
- Code simplification
8
9
- Composition
10
+
- Functional decomposition
11
+
- Reusability
9
12
---
10
13
11
14
## Also known as
12
15
13
16
* Function Chaining
14
-
* Function Pipelining
15
-
* Functional Composition
17
+
* Higher-Order Function Wrapping
16
18
17
19
## Intent of Function Composition Design Pattern
18
20
19
-
The Function Composition design pattern in Java enables the creation of complex functions by combining simpler ones. This enhances modular code and reusability, crucial for maintainable software development.
21
+
Combine multiple small functions into a single operation that executes them in a sequence, producing a new function as the result.
20
22
21
23
## Detailed Explanation of Function Composition Pattern with Real-World Examples
22
24
@@ -40,24 +42,24 @@ Sequence diagram
40
42
41
43
## Programmatic Example of Function Composition Pattern in Java
42
44
43
-
In the functional programming paradigm, function composition is a powerful technique. For instance, in Java, you can use higher-order functions to compose operations like multiplying and squaring numbers.
45
+
In the functional programming paradigm, function composition is a powerful technique. For instance, in Java, you can use higher-order functions to combine operations like multiplying and squaring numbers.
44
46
45
47
Using Java's functional interfaces, we can define simple functions and compose them. Here's how function composition works in Java.
46
48
47
-
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.
49
+
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:
48
50
49
51
```java
50
52
Function<Integer, Integer> timesTwo = x -> x *2;
51
53
Function<Integer, Integer> square = x -> x * x;
52
54
```
53
55
54
-
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.
56
+
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.
62
+
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:
61
63
62
64
```java
63
65
publicstaticvoid main(String[] args) {
@@ -84,10 +86,9 @@ This example demonstrates how the Function Composition pattern can be used to cr
84
86
85
87
Use the Function Composition pattern when:
86
88
87
-
* You want to create a pipeline of operations in Java. This enhances code clarity and quality by structuring complex logic into simpler, reusable components.
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.
89
+
* When you want to build complex transformations by chaining smaller, reusable functions in Java.
90
+
* When the logic is best expressed through a series of operations that naturally feed one into another.
91
+
* When you want to reduce code duplication and improve readability by isolating each operation in its own function.
91
92
92
93
## Function Composition Pattern Java Tutorials
93
94
@@ -96,31 +97,28 @@ Use the Function Composition pattern when:
96
97
97
98
## Real-World Applications of Function Composition Pattern in Java
98
99
99
-
* Stream processing in Java 8 and above
100
-
*Query builders in ORM libraries
101
-
*Middleware composition in web frameworks
100
+
*Java’s Stream API, where map and filter are composed for data transformations.
101
+
*Google Guava’s Function utilities.
102
+
*Apache Commons libraries that provide utilities for chaining functions.
102
103
103
104
## Benefits and Trade-offs of Function Composition Pattern
104
105
105
106
Benefits:
106
107
107
-
* High reusability of composed functions.
108
-
* Increased modularity, making complex functions easier to understand and maintain.
109
-
* Flexible and dynamic creation of function pipelines at runtime.
110
-
* Enhances readability by structuring code in a linear, declarative manner.
111
-
* Facilitates easier testing of individual functions.
108
+
* Encourages highly modular and reusable code.
109
+
* Simplifies complex logic by breaking it down into smaller, testable units.
110
+
* Makes the code more expressive and easier to maintain.
112
111
113
112
Trade-offs:
114
113
115
-
*Potentially higher complexity when debugging composed functions.
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.
114
+
*Excessive chaining can reduce readability if taken too far.
115
+
*May introduce performance overhead due to multiple function calls.
116
+
*Errors can be harder to trace in a deeply composed function pipeline.
118
117
119
118
## Related Java Design Patterns
120
119
121
120
*[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.
121
+
*[Composite](https://java-design-patterns.com/patterns/composite/): Also deals with combining smaller components, though it is typically about object structure rather than function operations.
0 commit comments