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:
Ilkka Seppälä
2024-06-08 19:54:44 +03:00
committed by GitHub
parent cb946c0cdc
commit 6cd2d0353a
219 changed files with 3308 additions and 2819 deletions
+32 -28
View File
@@ -1,19 +1,21 @@
---
title: Monad
title: "Monad Pattern in Java: Mastering Functional Programming Paradigms"
shortTitle: Monad
description: "Learn how to implement the Monad design pattern in Java for functional programming. Discover its benefits, real-world examples, and best practices to enhance code readability and error handling."
category: Functional
language: en
tag:
- Abstraction
- Accumulation
- Decoupling
- Encapsulation
- Functional decomposition
- Generic
- Idiom
- Instantiation
- Interface
- Layered architecture
- Object composition
- Abstraction
- Accumulation
- Decoupling
- Encapsulation
- Functional decomposition
- Generic
- Idiom
- Instantiation
- Interface
- Layered architecture
- Object composition
---
## Also known as
@@ -21,15 +23,15 @@ tag:
* Computation Wrapper
* Monadic Interface
## Intent
## Intent of Monad Design Pattern
To provide a mechanism for encapsulating computations or side effects, enabling the chaining of operations while managing context and data flow in a side-effect-free manner.
The Monad design pattern in Java provides a mechanism for encapsulating computations or side effects, enabling the chaining of operations while managing context and data flow in a side-effect-free manner.
## Explanation
## Detailed Explanation of Monad Pattern with Real-World Examples
Real-world example
> Consider a real-world analogous example of a monad with a restaurant meal ordering process. In this scenario, each step of selecting a dish, adding sides, and choosing a drink can be thought of as a monadic operation. Each operation encapsulates the current state of the order (e.g., main dish chosen) and allows for the next choice (e.g., selecting a side) without exposing the complexity of the entire order's details to the customer.
> Consider a real-world example of a monad in Java with a restaurant meal ordering process. This encapsulation and chaining allow for a clean, error-managed progression, similar to how monads handle data and operations in functional programming. In this scenario, each step of selecting a dish, adding sides, and choosing a drink can be thought of as a monadic operation. Each operation encapsulates the current state of the order (e.g., main dish chosen) and allows for the next choice (e.g., selecting a side) without exposing the complexity of the entire order's details to the customer.
>
> Just like in a functional monad, if any step fails (like an unavailable dish), the entire process can be halted or redirected without throwing exceptions, maintaining a smooth flow. This encapsulation and chaining allow for a clean, error-managed progression from choosing the main dish to completing the full meal order, akin to how monads handle data and operations in functional programming. This approach ensures a consistent experience, where every choice builds on the previous one in a controlled manner.
@@ -41,11 +43,9 @@ Wikipedia says
> In functional programming, a monad is a structure that combines program fragments (functions) and wraps their return values in a type with additional computation. In addition to defining a wrapping monadic type, monads define two operators: one to wrap a value in the monad type, and another to compose together functions that output values of the monad type (these are known as monadic functions). General-purpose languages use monads to reduce boilerplate code needed for common operations (such as dealing with undefined values or fallible functions, or encapsulating bookkeeping code). Functional languages use monads to turn complicated sequences of functions into succinct pipelines that abstract away control flow, and side effects.
**Programmatic Example**
## Programmatic Example of Monad Pattern in Java
Heres the Monad implementation in Java.
The `Validator` takes an object, validates it against specified predicates, and collects any validation errors. The `validate` method allows you to add validation steps, and the `get` method either returns the validated object or throws an `IllegalStateException` with a list of validation exceptions if any of the validation steps fail.
Heres the Monad implementation in Java. The `Validator` class encapsulates an object and performs validation steps in a monadic fashion, showcasing the benefits of using the Monad pattern for error handling and state management.
```java
public class Validator<T> {
@@ -119,24 +119,26 @@ Console output:
15:06:17.679 [main] INFO com.iluwatar.monad.App -- User[name=user, age=24, sex=FEMALE, email=foobar.com]
```
## Applicability
## When to Use the Monad Pattern in Java
* Consistent and unified error handling is required without relying on exceptions.
The Monad design pattern is applicable when
* Consistent and unified error handling is required without relying on exceptions, particularly in functional programming paradigms.
* Asynchronous computations need clear and maintainable chaining.
* State needs to be managed and encapsulated within functional flows.
* Dependencies and lazy evaluations are to be handled cleanly and efficiently.
## Tutorials
## Monad Pattern Java Tutorials
* [Design Pattern Reloaded (Remi Forax)](https://youtu.be/-k2X7guaArU)
## Known Uses
## Real-World Applications of Monad Pattern in Java
* Optional in Java's standard library for handling potential absence of values.
* Stream for constructing functional pipelines to operate on collections.
* Frameworks like Vavr, providing functional programming enhancements for Java.
* Frameworks like Vavr enhance functional programming in Java by providing monadic constructs for better code maintainability.
## Consequences
## Benefits and Trade-offs of Monad Pattern
Benefits:
@@ -151,13 +153,15 @@ Trade-offs:
* May introduce performance overhead due to additional abstraction layers.
* Debugging can be difficult due to less transparent operational flow.
## Related Patterns
## Related Java Design Patterns
Related design patterns to monads in Java include
* [Factory Method](https://java-design-patterns.com/patterns/factory-method/): Similar in that it helps instantiate monads, encapsulating object creation logic.
* [Command](https://java-design-patterns.com/patterns/command/): Also encapsulates operations, but monads add context management to the mix.
* [Decorator](https://java-design-patterns.com/patterns/decorator/): Dynamically enhances functionalities, whereas monads use static typing for consistent composability.
## Credits
## References and Credits
* [Functional Programming in Java](https://amzn.to/3JUIc5Q)
* [Java 8 in Action: Lambdas, Streams, and functional-style programming](https://amzn.to/3QCmGXs)