tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Design Patterns > Java Design Patterns > Mediator

Mediator 

Mediator comes under Behavioral design pattern. It defines an interface for communication among Colleague objects and encapsulates the communication logic.

Behaviour & Advantages

  • Encapsulates the communication logic.
  • Mediator promotes loose coupling by keeping objects from referring to each other explicitly.
  • Colleague objects need not know each other explicitly.
Participants
  • Mediator
    Abstract interface for communication among Colleague objects.
  • Concrete Mediator
    An implementation of Mediator which knows all Colleague objects. It keeps a reference to Colleagues and coordinates communication among them.
  • Colleague
    Abstract interface for Colleagues.
  • Concrete Colleague
    Objects which require communication with other Colleagues. It keeps a reference to Mediator and communicates with other Colleagues.

This example shows an organization where Employees like to communicate each other regarding a marriage invitation through a Mediator. Each employee doesn't require to know all other Employees directly.

File Name  :  
com/bethecoder/tutorials/dp/mediator/IMediator.java 
   
package com.bethecoder.tutorials.dp.mediator;

public interface IMediator {

  /**
   * Register employees with this mediator
   */
  public void register(IEmployee [] colleagues);

  /**
   * Broad cast message from employee based on some condition. 
   */
  public void sendMessage(IEmployee fromColleague, String message);

}
   

A Concrete mediator implementation which invites only the employees earning more salary than the message sender.

File Name  :  
com/bethecoder/tutorials/dp/mediator/RichEmployeeMediator.java 
   
package com.bethecoder.tutorials.dp.mediator;

import java.util.ArrayList;
import java.util.List;

/**
 * Broadcasts message to an employee if his salary
 * is greater than the message sender.
 
 * In mediator pattern the communication between 
 * objects is encapsulated and driven by mediator.
 */
public class RichEmployeeMediator implements IMediator {

  private List<IEmployee> empList = new ArrayList<IEmployee>();
  
  @Override
  public void register(IEmployee [] colleagues) {
    
    for (int i = ; i < colleagues.length ; i ++ ) {
      colleagues[i].setMediator(this);
      empList.add(colleagues[i]);
    }
    
  }

  @Override
  public void sendMessage(IEmployee fromColleague, String message) {
    
    for (int i = ; i < empList.size() ; i ++) {
      if (empList.get(i).getSalary() > fromColleague.getSalary()) {
        empList.get(i).onMessage(message);
      }
    }
  }

}
   

A Concrete mediator implementation which invites only the employees earning less salary than the message sender.

File Name  :  
com/bethecoder/tutorials/dp/mediator/PoorEmployeeMediator.java 
   
package com.bethecoder.tutorials.dp.mediator;

import java.util.ArrayList;
import java.util.List;

/**
 * Broadcasts message to an employee if his salary
 * is less than the message sender.
 
 * In mediator pattern the communication between 
 * objects is encapsulated and driven by mediator.
 */
public class PoorEmployeeMediator implements IMediator {

  private List<IEmployee> empList = new ArrayList<IEmployee>();
  
  @Override
  public void register(IEmployee [] colleagues) {
    
    for (int i = ; i < colleagues.length ; i ++ ) {
      colleagues[i].setMediator(this);
      empList.add(colleagues[i]);
    }
    
  }

  @Override
  public void sendMessage(IEmployee fromColleague, String message) {
    
    for (int i = ; i < empList.size() ; i ++) {
      if (empList.get(i).getSalary() < fromColleague.getSalary()) {
        empList.get(i).onMessage(message);
      }
    }
  }

}
   

Employee colleague interface is shown below,

File Name  :  
com/bethecoder/tutorials/dp/mediator/IEmployee.java 
   
package com.bethecoder.tutorials.dp.mediator;

public interface IEmployee {

  /**
   * Get salary of this employee
   */
  public int getSalary();
  
  /**
   * Set the mediator 
   */
  public void setMediator(IMediator mediator);

  /**
   * Receive a message from another employee 
   */
  public void onMessage(String message);
  
}
   

The Concrete colleague implementations Employee, Manager and CEO are shown below,

File Name  :  
com/bethecoder/tutorials/dp/mediator/Employee.java 
   
package com.bethecoder.tutorials.dp.mediator;

public class Employee implements IEmployee {
  private String name;
  private String designation;
  private int salary;
  private IMediator mediator;
  
  public Employee(String name, int salary) {
    super();
    this.name = name;
    this.salary = salary;
    setDesignation("Employee");
  }
  
  @Override
  public void onMessage(String message) {
    System.out.println("INBOX(" this ") : " + message);
  }

  @Override
  public void setMediator(IMediator mediator) {
    this.mediator = mediator;
  }
  
  public void sendMarriageInvitation() {
    this.mediator.sendMessage(this, 
        "Please come to my marriage - by " this);
  }

  @Override
  public int getSalary() {
    return salary;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getDesignation() {
    return designation;
  }

  public void setDesignation(String designation) {
    this.designation = designation;
  }

  public IMediator getMediator() {
    return mediator;
  }

  public void setSalary(int salary) {
    this.salary = salary;
  }
  
  public String toString() {
    return designation + "[" + name + ", "+ salary + "]";
  }

}
   

File Name  :  
com/bethecoder/tutorials/dp/mediator/Manager.java 
   
package com.bethecoder.tutorials.dp.mediator;

public class Manager extends Employee {

  public Manager(String name, int salary) {
    super(name, salary);
    setDesignation("Manager");
  }
}
   

File Name  :  
com/bethecoder/tutorials/dp/mediator/CEO.java 
   
package com.bethecoder.tutorials.dp.mediator;

public class CEO extends Employee {

  public CEO(String name, int salary) {
    super(name, salary);
    setDesignation("CEO");
  }
}
   

Mediator usage is shown below,

File Name  :  
com/bethecoder/tutorials/dp/mediator/Test.java 
   
package com.bethecoder.tutorials.dp.mediator;

public class Test {

  /**
   @param args
   */
  public static void main(String[] args) {

    CEO emp1 = new CEO("RAM"128000);
    Manager emp2 = new Manager("RAJ"48000);
    Manager emp3 = new Manager("KIRAN"28000);
    Employee emp4 = new Employee("ARUN"16000);
    Employee emp5 = new Employee("PRASAD"44000);
    Employee emp6 = new Employee("MANO"9000);
    Employee emp7 = new Employee("ARJUN"14000);
    Employee emp8 = new Employee("ABHI"12000);
    
    /**
     * Send invitation to all rich employees than sender.
     */
    IMediator mediator = new RichEmployeeMediator();
    mediator.register(new Employee [] { 
      emp1, emp2, emp3, emp4, emp5, emp6, emp7, emp8
    });
    emp4.sendMarriageInvitation();
    
    System.out.println();
    
    /**
     * Send invitation to all poor employees than sender.
     */
    mediator = new PoorEmployeeMediator();
    mediator.register(new Employee [] { 
        emp1, emp2, emp3, emp4, emp5, emp6, emp7, emp8
    });
    emp4.sendMarriageInvitation();
  }

}
   

It gives the following output,
INBOX(CEO[RAM, 128000]) : Please come to my marriage - by Employee[ARUN, 16000]
INBOX(Manager[RAJ, 48000]) : Please come to my marriage - by Employee[ARUN, 16000]
INBOX(Manager[KIRAN, 28000]) : Please come to my marriage - by Employee[ARUN, 16000]
INBOX(Employee[PRASAD, 44000]) : Please come to my marriage - by Employee[ARUN, 16000]

INBOX(Employee[MANO, 9000]) : Please come to my marriage - by Employee[ARUN, 16000]
INBOX(Employee[ARJUN, 14000]) : Please come to my marriage - by Employee[ARUN, 16000]
INBOX(Employee[ABHI, 12000]) : Please come to my marriage - by Employee[ARUN, 16000]


                    Print

   



Books Recommendation

 
 
bl  br