Skip to content

Commit 8ca487e

Browse files
committed
docs: update function composition pattern
1 parent ce2e5f7 commit 8ca487e

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

function-composition/README.md

+22-24
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ description: "Learn about the Function Composition design pattern in Java. Disco
55
category: Functional
66
language: en
77
tag:
8+
- Code simplification
89
- Composition
10+
- Functional decomposition
11+
- Reusability
912
---
1013

1114
## Also known as
1215

1316
* Function Chaining
14-
* Function Pipelining
15-
* Functional Composition
17+
* Higher-Order Function Wrapping
1618

1719
## Intent of Function Composition Design Pattern
1820

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.
2022

2123
## Detailed Explanation of Function Composition Pattern with Real-World Examples
2224

@@ -40,24 +42,24 @@ Sequence diagram
4042

4143
## Programmatic Example of Function Composition Pattern in Java
4244

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.
4446

4547
Using Java's functional interfaces, we can define simple functions and compose them. Here's how function composition works in Java.
4648

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:
4850

4951
```java
5052
Function<Integer, Integer> timesTwo = x -> x * 2;
5153
Function<Integer, Integer> square = x -> x * x;
5254
```
5355

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:
5557

5658
```java
5759
Function<Integer, Integer> composedFunction = FunctionComposer.composeFunctions(timesTwo, square);
5860
```
5961

60-
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:
6163

6264
```java
6365
public static void main(String[] args) {
@@ -84,10 +86,9 @@ This example demonstrates how the Function Composition pattern can be used to cr
8486

8587
Use the Function Composition pattern when:
8688

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.
9192

9293
## Function Composition Pattern Java Tutorials
9394

@@ -96,31 +97,28 @@ Use the Function Composition pattern when:
9697

9798
## Real-World Applications of Function Composition Pattern in Java
9899

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.
102103

103104
## Benefits and Trade-offs of Function Composition Pattern
104105

105106
Benefits:
106107

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.
112111

113112
Trade-offs:
114113

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.
118117

119118
## Related Java Design Patterns
120119

121120
* [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.
124122

125123
## References and Credits
126124

function-composition/src/main/java/com/iluwatar/function/composition/App.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
package com.iluwatar.function.composition;
2626

2727
import java.util.function.Function;
28-
import org.slf4j.LoggerFactory;
28+
import lombok.extern.slf4j.Slf4j;
2929

3030
/** Main application class to demonstrate the use of function composition. */
31+
@Slf4j
3132
public class App {
3233

3334
/**
@@ -36,14 +37,13 @@ public class App {
3637
* @param args command line arguments (not used)
3738
*/
3839
public static void main(String[] args) {
39-
final var logger = LoggerFactory.getLogger(App.class);
4040
Function<Integer, Integer> timesTwo = x -> x * 2;
4141
Function<Integer, Integer> square = x -> x * x;
4242

4343
Function<Integer, Integer> composedFunction =
4444
FunctionComposer.composeFunctions(timesTwo, square);
4545

4646
int result = composedFunction.apply(3);
47-
logger.info("Result of composing 'timesTwo' and 'square' functions applied to 3 is: " + result);
47+
LOGGER.info("Result of composing 'timesTwo' and 'square' functions applied to 3 is: " + result);
4848
}
4949
}

function-composition/src/main/java/com/iluwatar/function/composition/FunctionComposer.java

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
*/
3333
public class FunctionComposer {
3434

35+
private FunctionComposer() {}
36+
3537
/**
3638
* Composes two functions where the output of the first function becomes the input of the second
3739
* function.

function-composition/src/test/java/com/iluwatar/function/composition/FunctionComposerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.junit.jupiter.api.Test;
3131

3232
/** Test class for FunctionComposer. */
33-
public class FunctionComposerTest {
33+
class FunctionComposerTest {
3434

3535
/** Tests the composition of two functions. */
3636
@Test

0 commit comments

Comments
 (0)