mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-16 14:23:05 +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:
+17
-15
@@ -1,23 +1,25 @@
|
||||
---
|
||||
title: Mediator
|
||||
title: "Mediator Pattern in Java: Simplifying Object Communications in Complex Systems"
|
||||
shortTitle: Mediator
|
||||
description: "Learn how the Mediator Design Pattern reduces complexity in object communication and improves system maintainability with a centralized mediator in Java. Explore examples and implementation."
|
||||
category: Behavioral
|
||||
language: en
|
||||
tag:
|
||||
- Decoupling
|
||||
- Gang Of Four
|
||||
- Messaging
|
||||
- Object composition
|
||||
- Decoupling
|
||||
- Gang Of Four
|
||||
- Messaging
|
||||
- Object composition
|
||||
---
|
||||
|
||||
## Also known as
|
||||
|
||||
* Controller
|
||||
|
||||
## Intent
|
||||
## Intent of Mediator Design Pattern
|
||||
|
||||
The Mediator design pattern is intended to reduce the complexity of communication between multiple objects or classes in a system. It achieves this by providing a centralized mediator class that handles the interactions between different classes, thus reducing their direct dependencies on each other.
|
||||
|
||||
## Explanation
|
||||
## Detailed Explanation of Mediator Pattern with Real-World Examples
|
||||
|
||||
Real-world example
|
||||
|
||||
@@ -31,9 +33,9 @@ Wikipedia says
|
||||
|
||||
> In software engineering, the mediator pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioral pattern due to the way it can alter the program's running behavior. In object-oriented programming, programs often consist of many classes. Business logic and computation are distributed among these classes. However, as more classes are added to a program, especially during maintenance and/or refactoring, the problem of communication between these classes may become more complex. This makes the program harder to read and maintain. Furthermore, it can become difficult to change the program, since any change may affect code in several other classes. With the mediator pattern, communication between objects is encapsulated within a mediator object. Objects no longer communicate directly with each other, but instead communicate through the mediator. This reduces the dependencies between communicating objects, thereby reducing coupling.
|
||||
|
||||
**Programmatic Example**
|
||||
## Programmatic Example of Mediator Pattern in Java
|
||||
|
||||
In this example, the mediator encapsulates how a set of objects interact. Instead of referring to each other directly they use the mediator interface.
|
||||
In this example, the mediator encapsulates how a set of objects interact. Instead of referring to each other directly, they use the mediator interface.
|
||||
|
||||
The party members `Rogue`, `Wizard`, `Hobbit`, and `Hunter` all inherit from the `PartyMemberBase`implementing the `PartyMember` interface.
|
||||
|
||||
@@ -173,11 +175,11 @@ Here's the console output from running the example.
|
||||
14:05:15.083 [main] INFO com.iluwatar.mediator.PartyMemberBase -- Rogue arrives for dinner
|
||||
```
|
||||
|
||||
## Class diagram
|
||||
## Detailed Explanation of Mediator Pattern with Real-World Examples
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
## When to Use the Mediator Pattern in Java
|
||||
|
||||
Use the Mediator pattern when
|
||||
|
||||
@@ -185,7 +187,7 @@ Use the Mediator pattern when
|
||||
* Reusing an object is difficult because it refers to and communicates with many other objects
|
||||
* A behavior that's distributed between several classes should be customizable without a lot of subclassing
|
||||
|
||||
## Known uses
|
||||
## Real-World Applications of Mediator Pattern in Java
|
||||
|
||||
* All scheduleXXX() methods of [java.util.Timer](http://docs.oracle.com/javase/8/docs/api/java/util/Timer.html)
|
||||
* [java.util.concurrent.Executor#execute()](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html#execute-java.lang.Runnable-)
|
||||
@@ -195,7 +197,7 @@ Use the Mediator pattern when
|
||||
* Java Message Service (JMS) uses mediators to handle message exchanges between clients and servers.
|
||||
* JavaBeans property change support class (java.beans.PropertyChangeSupport) acts as a mediator by handling communication between beans regarding property changes.
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of Mediator Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
@@ -206,13 +208,13 @@ Trade-offs:
|
||||
|
||||
* Mediator can become a god object coupled with all classes in the system, gaining too much responsibility and complexity.
|
||||
|
||||
## Related Patterns
|
||||
## Related Java Design Patterns
|
||||
|
||||
* [Observer](https://java-design-patterns.com/patterns/observer/): Often used together, where the mediator pattern can use the observer pattern to notify various objects about state changes. Mediators effectively act as a channel of communication managed by an observer.
|
||||
* [Facade](https://java-design-patterns.com/patterns/facade/): Mediator simplifies communication between components, similar to how a facade simplifies a subsystem interface, but a mediator’s colleagues can communicate back to the mediator.
|
||||
* [Command](https://java-design-patterns.com/patterns/command/): Commands can be mediated as they are dispatched to their receivers, encapsulating a request as an object.
|
||||
|
||||
## Credits
|
||||
## References and Credits
|
||||
|
||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
|
||||
* [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/49NGldq)
|
||||
|
||||
Reference in New Issue
Block a user