diff --git a/mediator/README.md b/mediator/README.md index 16ff6ec07..c84e38b70 100644 --- a/mediator/README.md +++ b/mediator/README.md @@ -3,50 +3,39 @@ title: Mediator category: Behavioral language: en tag: - - Gang Of Four - - Decoupling + - Decoupling + - Gang Of Four + - Messaging + - Object composition --- +## Also known as + +* Controller + ## Intent -Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling -by keeping objects from referring to each other explicitly, and it lets you vary their interaction -independently. +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 Real-world example -> Rogue, wizard, hobbit, and hunter have decided to join their forces and travel in the same -> party. To avoid coupling each member with each other, they use the party interface to -> communicate with each other. +> Imagine an air traffic control system at a busy airport, where the air traffic controller acts as a mediator. In this scenario, numerous airplanes wish to take off, land, or navigate around the airport's airspace. Instead of each pilot communicating directly with every other pilot, which could lead to confusion and potential accidents, all communication goes through the air traffic controller. The controller receives requests, processes them, and gives clear, organized instructions to each pilot. This centralized system reduces the complexity of communications and ensures safety and efficiency in managing the airport's operations. This is analogous to the Mediator design pattern in software, where a central mediator class handles and coordinates the interaction between different objects or systems. In plain words -> Mediator decouples a set of classes by forcing their communications flow through a mediating -> object. +> Mediator decouples a set of classes by forcing their communications flow through a mediating object. 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. +> 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** -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. +The party members `Rogue`, `Wizard`, `Hobbit`, and `Hunter` all inherit from the `PartyMemberBase`implementing the `PartyMember` interface. ```java public interface PartyMember { @@ -200,8 +189,27 @@ Use the Mediator pattern when * submit() and invokeXXX() methods of [java.util.concurrent.ExecutorService](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html) * scheduleXXX() methods of [java.util.concurrent.ScheduledExecutorService](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledExecutorService.html) * [java.lang.reflect.Method#invoke()](http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#invoke-java.lang.Object-java.lang.Object...-) +* 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: + +* Reduces coupling between components of a program, fostering better organization and easier maintenance. +* Centralizes control. The mediator pattern centralizes the control logic, making it easier to comprehend and manage. + +Trade-offs: + +* Mediator can become a god object coupled with all classes in the system, gaining too much responsibility and complexity. + +## Related 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 -* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59) -* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b) +* [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) diff --git a/mediator/src/main/java/com/iluwatar/mediator/Action.java b/mediator/src/main/java/com/iluwatar/mediator/Action.java index 307aba9f5..61f70f0e6 100644 --- a/mediator/src/main/java/com/iluwatar/mediator/Action.java +++ b/mediator/src/main/java/com/iluwatar/mediator/Action.java @@ -24,6 +24,8 @@ */ package com.iluwatar.mediator; +import lombok.Getter; + /** * Action enumeration. */ @@ -35,6 +37,7 @@ public enum Action { NONE("", ""); private final String title; + @Getter private final String description; Action(String title, String description) { @@ -42,10 +45,6 @@ public enum Action { this.description = description; } - public String getDescription() { - return description; - } - public String toString() { return title; } diff --git a/mediator/src/test/java/com/iluwatar/mediator/PartyMemberTest.java b/mediator/src/test/java/com/iluwatar/mediator/PartyMemberTest.java index 72270f2f7..4ae0c11e1 100644 --- a/mediator/src/test/java/com/iluwatar/mediator/PartyMemberTest.java +++ b/mediator/src/test/java/com/iluwatar/mediator/PartyMemberTest.java @@ -80,7 +80,7 @@ class PartyMemberTest { for (final var action : Action.values()) { member.partyAction(action); - assertEquals(member.toString() + " " + action.getDescription(), appender.getLastMessage()); + assertEquals(member + " " + action.getDescription(), appender.getLastMessage()); } assertEquals(Action.values().length, appender.getLogSize()); @@ -99,11 +99,11 @@ class PartyMemberTest { final var party = mock(Party.class); member.joinedParty(party); - assertEquals(member.toString() + " joins the party", appender.getLastMessage()); + assertEquals(member + " joins the party", appender.getLastMessage()); for (final var action : Action.values()) { member.act(action); - assertEquals(member.toString() + " " + action.toString(), appender.getLastMessage()); + assertEquals(member + " " + action.toString(), appender.getLastMessage()); verify(party).act(member, action); }