mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 00:58:24 +00:00
docs: Content SEO updates (#2990)
* update yaml frontmatter format * update abstract document * update abstract factory * use the new pattern template * acyclic visitor seo * adapter seo * ambassador seo * acl seo * aaa seo * async method invocation seo * balking seo * bridge seo * builder seo * business delegate and bytecode seo * caching seo * callback seo * chain seo * update headings * circuit breaker seo * client session + collecting parameter seo * collection pipeline seo * combinator SEO * command seo * cqrs seo * commander seo * component seo * composite seo * composite entity seo * composite view seo * context object seo * converter seo * crtp seo * currying seo * dao seo * data bus seo * data locality seo * data mapper seo * dto seo * decorator seo * delegation seo * di seo * dirty flag seo * domain model seo * double buffer seo * double checked locking seo * double dispatch seo * dynamic proxy seo * event aggregator seo * event-based asynchronous seo * eda seo * event queue seo * event sourcing seo * execute around seo * extension objects seo * facade seo * factory seo * factory kit seo * factory method seo * fanout/fanin seo * feature toggle seo * filterer seo * fluent interface seo * flux seo * flyweight seo * front controller seo * function composition seo * game loop seo * gateway seo * guarded suspension seo * half-sync/half-async seo * health check seo * hexagonal seo * identity map seo * intercepting filter seo * interpreter seo * iterator seo * layers seo * lazy loading seo * leader election seo * leader/followers seo * lockable object seo * rename and add seo for marker interface * master-worker seo * mediator seo * memento seo * metadata mapping seo * microservice aggregator seo * api gw seo * microservices log aggregration seo * mvc seo * mvi seo * mvp seo * mvvm seo * monad seo * monitor seo * monostate seo * multiton seo * mute idiom seo * naked objects & notification seo * null object seo * object mother seo * object pool seo * observer seo * optimistic locking seo * page controller seo * page object seo * parameter object seo * partial response seo * pipeline seo * poison pill seo * presentation model seo * private class data seo * producer-consumer seo * promise seo * property seo * prototype seo * proxy seo * queue-based load leveling seo * reactor seo * registry seo * repository seo * RAII seo * retry seo * role object seo * saga seo * separated interface seo * serialized entity seo * serialized lob seo * servant seo * server session seo * service layer seo * service locator seo * service to worker seo * sharding seo * single table inheritance seo * singleton seo * spatial partition seo * special case seo * specification seo * state seo * step builder seo * strangler seo * strategy seo * subclass sandbox seo * table module seo * template method seo * throttling seo * tolerant reader seo * trampoline seo * transaction script seo * twin seo * type object seo * unit of work seo * update method seo * value object seo * version number seo * virtual proxy seo * visitor seo * seo enhancements * seo improvements * SEO enhancements * SEO improvements * SEO additions * SEO improvements * more SEO improvements * rename hexagonal + SEO improvements * SEO improvements * more SEO stuff * SEO improvements * SEO optimizations * SEO enhancements * enchance SEO * improve SEO * SEO improvements * update headers
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
---
|
||||
title: Function Composition
|
||||
title: "Function Composition Pattern in Java: Crafting Elegant Functional Pipelines"
|
||||
shortTitle: Function Composition
|
||||
description: "Learn about the Function Composition design pattern in Java. Discover how to create complex functions by combining simpler ones, enhancing code modularity and reusability. Explore real-world examples, benefits, and applications."
|
||||
category: Functional
|
||||
language: en
|
||||
tag:
|
||||
- Code simplification
|
||||
- Composition
|
||||
- Decoupling
|
||||
- Functional decomposition
|
||||
- Lambda
|
||||
- Reusability
|
||||
- Code simplification
|
||||
- Composition
|
||||
- Decoupling
|
||||
- Functional decomposition
|
||||
- Lambda
|
||||
- Reusability
|
||||
---
|
||||
|
||||
## Also known as
|
||||
@@ -17,11 +19,11 @@ tag:
|
||||
* Function Pipelining
|
||||
* Functional Composition
|
||||
|
||||
## Intent
|
||||
## Intent of Function Composition Design Pattern
|
||||
|
||||
To enable creating complex functions by composing simpler ones, enhancing modularity and reusability of function-based logic.
|
||||
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.
|
||||
|
||||
## Explanation
|
||||
## Detailed Explanation of Function Composition Pattern with Real-World Examples
|
||||
|
||||
Real-world example
|
||||
|
||||
@@ -37,7 +39,11 @@ Wikipedia says
|
||||
|
||||
> 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.
|
||||
|
||||
**Programmatic Example**
|
||||
## Programmatic Example of Function Composition Pattern in Java
|
||||
|
||||
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.
|
||||
|
||||
Using Java's functional interfaces, we can define simple functions and compose them. Here's how function composition works in Java.
|
||||
|
||||
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.
|
||||
|
||||
@@ -75,32 +81,31 @@ Result of composing 'timesTwo' and 'square' functions applied to 3 is: 36
|
||||
|
||||
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.
|
||||
|
||||
## Sequence diagram
|
||||
## Function Composition Pattern Sequence diagram
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
## When to Use the Function Composition Pattern in Java
|
||||
|
||||
Use the Function Composer pattern when:
|
||||
Use the Function Composition pattern when:
|
||||
|
||||
* You want to create a pipeline of operations where the output of one function is the input to another.
|
||||
* You need to enhance the clarity and quality of your code by structuring complex function logic into simpler, reusable components.
|
||||
* You want to create a pipeline of operations in Java. This enhances code clarity and quality by structuring complex logic into simpler, reusable components.
|
||||
* You are working in a functional programming environment or a language that supports higher-order functions.
|
||||
* When you want to avoid deep nesting of function calls and instead build a pipeline of operations.
|
||||
* When aiming to promote immutability and side-effect-free functions in your design.
|
||||
|
||||
## Tutorials
|
||||
## Function Composition Pattern Java Tutorials
|
||||
|
||||
* [Function Composition in Java (Medium)](https://functionalprogramming.medium.com/function-composition-in-java-beaf39426f52)
|
||||
* [Functional Programming in Java (Baeldung)](https://www.baeldung.com/java-functional-programming)
|
||||
|
||||
## Known uses
|
||||
## Real-World Applications of Function Composition Pattern in Java
|
||||
|
||||
* Stream processing in Java 8 and above
|
||||
* Query builders in ORM libraries
|
||||
* Middleware composition in web frameworks
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of Function Composition Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
@@ -116,13 +121,13 @@ Trade-offs:
|
||||
* Overhead from creating and managing multiple function objects in memory-intensive scenarios.
|
||||
* May require a paradigm shift for developers unfamiliar with functional programming concepts.
|
||||
|
||||
## Related patterns
|
||||
## Related Java Design Patterns
|
||||
|
||||
* [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.
|
||||
* [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.
|
||||
* [Strategy](https://java-design-patterns.com/patterns/strategy/) - Provides interchangeable functions (strategies), which can be composed in Functional Composition.
|
||||
|
||||
## Credits
|
||||
## References and Credits
|
||||
|
||||
* [Effective Java](https://amzn.to/4cGk2Jz)
|
||||
* [Functional Programming in Java](https://amzn.to/3JUIc5Q)
|
||||
|
||||
Reference in New Issue
Block a user