Mediator Design Pattern in Java


Mediator Pattern is used to reduce the communication complexity between multiple objects and classes. This pattern provides a mediation class that typically handles communication between different classes and supports loose coupling to make the code easy to maintain. The mediator mode is a behavioral mode.

Introduction

Intent: Use a mediation object to encapsulate a series of object interactions. The mediator does not need to explicitly refer to each other, making them loosely coupled and can change them independently. Interaction between.

Main solution: There is a large number of associations between objects and objects, which will inevitably lead to the complexity of the system structure. If an object changes, we also need to track it. The associated objects are processed at the same time.

When to use: Multiple classes are coupled to each other to form a mesh structure.

How to solve: Separate the above network structure into a star structure.

Key Code: Object Communication between Colleague is encapsulated into a single class for processing.

Application example: 1. Before China joined the WTO, each country traded with each other and its structure was complicated. Now it is the mutual trade of various countries through the WTO. 2. Airport dispatch system. 3. The MVC framework, where C (controller) is the intermediary of M (model) and V (view).

Advantages: 1. Reduce the complexity of the class and convert one-to-many into one-to-one. 2. Decoupling between classes. 3. Meet the Dimit principle.

Disadvantages: Intermediaries can be huge and complex and difficult to maintain.

Usage scenarios: 1. There are relatively complex reference relationships between objects in the system, which makes the dependency structure between them confusing and difficult to reuse the object. 2, I want to encapsulate the behavior in multiple classes through an intermediate class, but do not want to generate too many subclasses.

Note: should not be used when the duties are confusing.

Implementation

We demonstrate the mediator mode through a chat room instance. In an example, multiple users can send messages to a chat room, which displays messages to all users. We will create two classes ChatRoom and User. User objects use the ChatRoom method to share their messages.

MediatorPatternDemo, our demo class uses the User object to display communication between them.

mediator_pattern_uml_diagram

Step 1

Create a mediation class.

ChatRoom.java

import java.util.Date; public class ChatRoom { public static void showMessage(User user, String message){ System.out.println(new Date().toString() + " [" + user.getName() +"] : " + message); } }

Step 3

Use the User object to display communication between them.

MediatorPatternDemo.java

public class MediatorPatternDemo { public static void main(String[] args) { User robert = new User("Robert"); User john = new User("John"); robert.sendMessage("Hi! John!"); john.sendMessage("Hello! Robert!"); } }

Step 4

Executing the program, outputting results:

Thu Jan 31 16:05:46 IST 2013 [Robert] : Hi! John!
Thu Jan 31 16:05:46 IST 2013 [John] : Hello! Robert!





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition