mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-12 20:22:28 +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:
+21
-18
@@ -1,27 +1,29 @@
|
||||
---
|
||||
title: Currying
|
||||
title: "Currying Pattern in Java: Enhancing Function Flexibility and Reusability"
|
||||
shortTitle: Currying
|
||||
description: "Learn about currying in Java, a technique to simplify functions by breaking them into a sequence of single-argument functions. Discover its applications, benefits, and examples in this comprehensive guide."
|
||||
category: Functional
|
||||
language: en
|
||||
tag:
|
||||
- Code simplification
|
||||
- Functional decomposition
|
||||
- Generic
|
||||
- Immutable
|
||||
- Code simplification
|
||||
- Functional decomposition
|
||||
- Generic
|
||||
- Immutable
|
||||
---
|
||||
|
||||
## Also known as
|
||||
|
||||
* Partial Function Application
|
||||
|
||||
## Intent
|
||||
## Intent of Currying Design Pattern
|
||||
|
||||
Currying decomposes a function that takes multiple arguments into a sequence of functions that each take a single argument. It helps in creating a higher-order function by partial application of its arguments.
|
||||
Currying decomposes a function that takes multiple arguments into a sequence of functions that each take a single argument. This technique is integral in functional programming, enabling the creation of higher-order functions through partial application of its arguments. Using currying in Java can lead to more modular, reusable, and maintainable code.
|
||||
|
||||
## Explanation
|
||||
## Detailed Explanation of Currying Pattern with Real-World Examples
|
||||
|
||||
Real-world example
|
||||
|
||||
> Currying in programming can be compared to an assembly line in a factory. Imagine a car manufacturing process where each station on the assembly line performs a specific task, such as installing the engine, painting the car, and adding the wheels. Each station takes a partially completed car and performs a single operation before passing it to the next station. Similarly, in currying, a function that requires multiple arguments is broken down into a series of functions, each taking a single argument and returning another function until all arguments are provided. This step-by-step processing simplifies complex tasks by dividing them into manageable, sequential operations.
|
||||
> Currying in programming can be compared to an assembly line in a factory. Imagine a car manufacturing process where each station on the assembly line performs a specific task, such as installing the engine, painting the car, and adding the wheels. Each station takes a partially completed car and performs a single operation before passing it to the next station. Similarly, in currying, a function that requires multiple arguments is broken down into a series of functions, each taking a single argument and returning another function until all arguments are provided. This step-by-step processing simplifies complex tasks by dividing them into manageable, sequential operations, which is especially useful in Java functional programming.
|
||||
|
||||
In plain words
|
||||
|
||||
@@ -31,7 +33,7 @@ Wikipedia says
|
||||
|
||||
> In mathematics and computer science, currying is the technique of translating a function that takes multiple arguments into a sequence of families of functions, each taking a single argument.
|
||||
|
||||
**Programmatic example**
|
||||
## Programmatic example of Currying Pattern in Java
|
||||
|
||||
Consider a librarian who wants to populate their library with books. The librarian wants functions which can create books corresponding to specific genres and authors. Currying makes this possible by writing a curried book builder function and utilising partial application.
|
||||
|
||||
@@ -184,25 +186,26 @@ Program output:
|
||||
09:04:52.506 [main] INFO com.iluwatar.currying.App -- Book{genre=SCIFI, author='Isaac Asimov', title='Foundation', publicationDate=1942-05-01}
|
||||
```
|
||||
|
||||
## Applicability
|
||||
## When to Use the Currying Pattern in Java
|
||||
|
||||
* When functions need to be called with some arguments preset.
|
||||
* When functions need to be called with some arguments preset in Java.
|
||||
* In functional programming languages or paradigms to simplify functions that take multiple arguments.
|
||||
* To improve code reusability and composability by breaking down functions into simpler, unary functions.
|
||||
* To improve code reusability and composability by breaking down functions into simpler, unary functions, enhancing the modularity of Java applications.
|
||||
|
||||
## Tutorials
|
||||
## Currying Pattern Java Tutorials
|
||||
|
||||
* [Currying in Java (Baeldung)](https://www.baeldung.com/java-currying)
|
||||
* [What Is Currying in Programming (Towards Data Science)](https://towardsdatascience.com/what-is-currying-in-programming-56fd57103431#:~:text=Currying%20is%20helpful%20when%20you,concise%2C%20and%20more%20readable%20solution.)
|
||||
* [Why the fudge should I use currying? (DailyJS)](https://medium.com/dailyjs/why-the-fudge-should-i-use-currying-84e4000c8743)
|
||||
|
||||
## Known uses
|
||||
## Real-World Applications of Currying Pattern in Java
|
||||
|
||||
* Functional programming languages like Haskell, Scala, and JavaScript.
|
||||
* Java programming, especially with lambda expressions and streams introduced in Java 8.
|
||||
* Event handling in UIs where a function with specific parameters needs to be triggered upon an event.
|
||||
* APIs that require configuration with multiple parameters.
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of Currying Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
@@ -217,13 +220,13 @@ Trade-offs:
|
||||
* Can be less intuitive for developers unfamiliar with functional programming concepts.
|
||||
* As shown in the programmatic example above, curried functions with several parameters have a cumbersome type signature in Java.
|
||||
|
||||
## Related patterns
|
||||
## Related Java Design Patterns
|
||||
|
||||
* Function Composition: Currying is often used in conjunction with function composition to enable more readable and concise code.
|
||||
* [Decorator](https://java-design-patterns.com/patterns/decorator/): While not the same, currying shares the decorator pattern's concept of wrapping functionality.
|
||||
* [Factory](https://java-design-patterns.com/patterns/factory/): Currying can be used to create factory functions that produce variations of a function with certain arguments preset.
|
||||
|
||||
## Credits
|
||||
## References and Credits
|
||||
|
||||
* [Functional Programming in Java: Harnessing the Power Of Java 8 Lambda Expressions](https://amzn.to/3TKeZPD)
|
||||
* [Java 8 in Action: Lambdas, Streams, and functional-style programming](https://amzn.to/3J6vEaW)
|
||||
|
||||
Reference in New Issue
Block a user