mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 12:58:37 +00:00
docs: update monad
This commit is contained in:
+64
-33
@@ -3,28 +3,35 @@ title: Monad
|
||||
category: Functional
|
||||
language: en
|
||||
tag:
|
||||
- Reactive
|
||||
- Abstraction
|
||||
- Accumulation
|
||||
- Decoupling
|
||||
- Encapsulation
|
||||
- Functional decomposition
|
||||
- Generic
|
||||
- Idiom
|
||||
- Instantiation
|
||||
- Interface
|
||||
- Layered architecture
|
||||
- Object composition
|
||||
---
|
||||
|
||||
## Also known as
|
||||
|
||||
* Computation Wrapper
|
||||
* Monadic Interface
|
||||
|
||||
## Intent
|
||||
|
||||
Monad pattern based on monad from linear algebra represents the way of chaining operations
|
||||
together step by step. Binding functions can be described as passing one's output to another's input
|
||||
basing on the 'same type' contract. Formally, monad consists of a type constructor M and two
|
||||
operations:
|
||||
bind - that takes monadic object and a function from plain object to monadic value and returns monadic value
|
||||
return - that takes plain type object and returns this object wrapped in a monadic value.
|
||||
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.
|
||||
|
||||
## Explanation
|
||||
|
||||
The Monad pattern provides a way to chain operations together and manage sequencing,
|
||||
side effects, and exception handling in a consistent manner.
|
||||
|
||||
Real-world example
|
||||
|
||||
> Consider a conveyor belt in a factory: items move from one station to another,
|
||||
> and each station performs a specific task, with the assurance that every task will be carried out,
|
||||
> even if some items are rejected at certain stations.
|
||||
> 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.
|
||||
>
|
||||
> 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.
|
||||
|
||||
In plain words
|
||||
|
||||
@@ -32,23 +39,13 @@ In plain words
|
||||
|
||||
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.
|
||||
> 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**
|
||||
|
||||
Here’s 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.
|
||||
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.
|
||||
|
||||
```java
|
||||
public class Validator<T> {
|
||||
@@ -107,8 +104,8 @@ And finally, a `User` object is validated for its name, email, and age using the
|
||||
|
||||
```java
|
||||
public static void main(String[] args) {
|
||||
var user = new User("user", 24, Sex.FEMALE, "foobar.com");
|
||||
LOGGER.info(Validator.of(user).validate(User::name, Objects::nonNull, "name is null")
|
||||
var user = new User("user", 24, Sex.FEMALE, "foobar.com");
|
||||
LOGGER.info(Validator.of(user).validate(User::name, Objects::nonNull, "name is null")
|
||||
.validate(User::name, name -> !name.isEmpty(), "name is empty")
|
||||
.validate(User::email, email -> !email.contains("@"), "email doesn't contains '@'")
|
||||
.validate(User::age, age -> age > 20 && age < 30, "age isn't between...").get()
|
||||
@@ -117,17 +114,51 @@ public static void main(String[] args) {
|
||||
```
|
||||
|
||||
## Class diagram
|
||||

|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
|
||||
Use the Monad in any of the following situations
|
||||
* Consistent and unified error handling is required without relying on exceptions.
|
||||
* 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.
|
||||
|
||||
* When you want to chain operations easily
|
||||
* When you want to apply each function regardless of the result of any of them
|
||||
|
||||
## Credits
|
||||
## Tutorials
|
||||
|
||||
* [Design Pattern Reloaded by Remi Forax](https://youtu.be/-k2X7guaArU)
|
||||
* [Brian Beckman: Don't fear the Monad](https://channel9.msdn.com/Shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads)
|
||||
* [Monad on Wikipedia](https://en.wikipedia.org/wiki/Monad_(functional_programming))
|
||||
|
||||
## Known Uses
|
||||
|
||||
* Optional<T> in Java's standard library for handling potential absence of values.
|
||||
* Stream<T> for constructing functional pipelines to operate on collections.
|
||||
* Frameworks like Vavr, providing functional programming enhancements for Java.
|
||||
|
||||
## Consequences
|
||||
|
||||
Benefits:
|
||||
|
||||
* Increases code readability and reduces boilerplate.
|
||||
* Encourages a declarative programming style.
|
||||
* Promotes immutability and thread safety.
|
||||
* Simplifies complex error handling and state management.
|
||||
|
||||
## Trade-offs
|
||||
|
||||
* Can be challenging for developers new to functional programming.
|
||||
* May introduce performance overhead due to additional abstraction layers.
|
||||
* Debugging can be difficult due to less transparent operational flow.
|
||||
|
||||
## Related Patterns
|
||||
|
||||
* [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
|
||||
|
||||
* [Functional Programming in Java](https://amzn.to/3JUIc5Q)
|
||||
* [Java 8 in Action: Lambdas, Streams, and functional-style programming](https://amzn.to/3QCmGXs)
|
||||
* [Real-World Software Development: A Project-Driven Guide to Fundamentals in Java](https://amzn.to/4btoN7U)
|
||||
|
||||
Reference in New Issue
Block a user