Thursday, 4 January 2018

Design Patterns | Mediator design pattern

Mediator pattern defines an object that encapsulates how a set of objects interact.

We generally to create lots of objects interacting one with each other to achieve a good object-oriented design. If the objects interact with each other directly, there is a chance of tight coupling.

The system components are tightly-coupled with each other that make higher maintainability cost and not hard to extend. Mediator pattern helps us to avoid tight-coupling of objects by introducing a layer in between so that the interaction between objects happens via the layer.

Mediator pattern provides a mediator between objects for communication and help in implementing lose-coupling between objects.

Before Mediator Design Pattern

After Mediator Design Pattern

Mediator - defines the interface for communication between Colleague objects.
ConcreteMediator - implements the Mediator interface and coordinates communication and transfer the messages between the colleague objects.
Colleague - defines the interface for communication with other Colleagues
ConcreteColleague - implements the Colleague interface and communicates with 
other Colleagues through its Mediator

Chat application
package designpattern.mediator;

public interface IChatMediator {
    public void sendMessage(IUser from, IUser to, String msg);

    void addUser(IUser user);

import java.util.ArrayList;
import java.util.List;
public class ChatMediator implements IChatMediator {
      private List<IUser> users;

      public ChatMediator() {
            this.users=new ArrayList<>();

      public void addUser(IUser user) {

      public void sendMessage(IUser from, IUser to, String msg) {

public abstract class IUser {
      protected IChatMediator mediator;
      protected String name;

      public IUser(IChatMediator med, String name){

      public abstract void send(IUser to, String msg);

      public abstract void receive(IUser from,String msg);

public class User extends IUser {
      public User(ChatMediator med, String name) {

      public void send(IUser to, String msg) {
            System.out.println(": Sending Message="+msg);
      public void receive(IUser from, String msg) {
            System.out.println("Delivered from "" to ";

public class ChatClient {
      public static void main(String[] args) {
            ChatMediator mediator = new ChatMediator();
            IUser john = new User(mediator"john");
            IUser micheal = new User(mediator"micheal");
            john.send(micheal"hello micheal!");
            micheal.send(john"hello john!");

john: Sending Message=hello micheal!
Delivered from john to micheal
micheal: Sending Message=hello john!
Delivered from micheal to john

Mediator Pattern usage in JDK
1. Java Message Service (JMS) uses Mediator pattern along with Observer pattern to allow applications to subscribe and publish data to other applications.

2. java.util.Timer class schedule (i.e scheduleAtFixedRate) methods.

3. Java Concurrency Executor execute() method.

4. java.lang.reflect.Method invoke() method.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...