Mediator Pattern is one of the behavioral patterns as it deals with the behavior of the program. Usually a program is made up of a large number of classes. So the logic and computation are distributed among these classes. As more classes are developed in a program, the number of classes are increased, thus arising the problem of communication among these classes and it becomes much difficult to read and maintain. Therefore, the mediator pattern comes in rescue to the complexity which comes in the coding when number of classes increase.

Mediator design pattern is very helpful in an enterprise application where multiple objects are interacting with each other. Mediator design pattern is used to provide a centralized communication medium among different objects. In this pattern, objects no longer communicate directly with each other in a tightly-coupled manner that makes maintainability cost higher and not flexible to extend easily, but instead communicate through the mediator. This reduces the dependencies between communicating objects, thereby implementing the lose-coupling.

For more info you can find at http://en.wikipedia.org/wiki/Mediator_pattern

Class Diagram

Mediator Pattern in Java
Example

The Java Message Service (JMS) API is a Java Message Oriented Middleware (MOM) API for sending messages between two or more clients. The JMS API supports two models. One is point-to-point and another is the publish-subscribe model. Publish-subscribe is an implementation of the mediator pattern. The messages can be publisehd based on a particular topic. The publisher has to create a subscription to which different subscribers may subscribe. Zero or more subscribers may subscribe to receive messages on a particular message topic. The publisher and the subscriber do not know one about each other, the subscriber can even be inactive. In this case the subscriber receives the messages when it will become active.

Implementation

Let’s take an example, in an IT organization there are several Accounts, under Account there are several projects etc. So a manager can be assigned to one or more Accounts or Projects. Suppose a manger looks after only one project and assigns tasks to team members. So when the manager assigns tasks to his/her team members, team members get those tasks and start working on those tasks.

Now we will look into the implementation step by step.

package com.roytuts.designpattern.mediator;

public interface ManagerMediator {

  void workOnTask(TeamMember member, String task);

  void recruitMember(TeamMember member);

}
package com.roytuts.designpattern.mediator;

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

public class ManagerMediatorImpl implements ManagerMediator {

  private List<TeamMember> members;

  public ManagerMediatorImpl() {
    this.members = new ArrayList<TeamMember>();
  }

  @Override
  public void workOnTask(TeamMember member, String task) {
    for (TeamMember mem : members) {
      if (mem != member) {
        mem.getWork(task);
      }
    }
  }

  @Override
  public void recruitMember(TeamMember member) {
    members.add(member);
  }

}
package com.roytuts.designpattern.mediator;

public abstract class TeamMember {

  protected ManagerMediator mediator;
  protected String memName;

  public TeamMember(ManagerMediator mediator, String memName) {
    this.mediator = mediator;
    this.memName = memName;
  }

  public ManagerMediator getMediator() {
    return mediator;
  }

  public void setMediator(ManagerMediator mediator) {
    this.mediator = mediator;
  }

  public String getMemName() {
    return memName;
  }

  public void setMemName(String memName) {
    this.memName = memName;
  }

  public abstract void assignWork(String task);

  public abstract void getWork(String task);

}
package com.roytuts.designpattern.mediator;

public class TeamMemberImpl extends TeamMember {

  public TeamMemberImpl(ManagerMediator mediator, String memName) {
    super(mediator, memName);
  }

  @Override
  public void assignWork(String task) {
    System.out.println(memName + " assigning task : " + task);
    mediator.workOnTask(this, task);
  }

  @Override
  public void getWork(String task) {
    System.out.println(memName + " got a task : " + task);
  }

}
package com.roytuts.designpattern.mediator;

public class MediatorPatternTest {

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

    ManagerMediatorImpl itMediator = new ManagerMediatorImpl();

    TeamMember saikat = new TeamMemberImpl(itMediator, "Saikat");
    TeamMember sumit = new TeamMemberImpl(itMediator, "Sumit");
    TeamMember soumen = new TeamMemberImpl(itMediator, "Soumen");
    TeamMember gourab = new TeamMemberImpl(itMediator, "Gourab");
    TeamMember debmalya = new TeamMemberImpl(itMediator, "Debmalya");
    TeamMember debabrata = new TeamMemberImpl(itMediator, "Debabrata");
    TeamMember liton = new TeamMemberImpl(itMediator, "Liton");

    itMediator.recruitMember(saikat);
    itMediator.recruitMember(sumit);
    itMediator.recruitMember(soumen);
    itMediator.recruitMember(gourab);
    itMediator.recruitMember(debmalya);
    itMediator.recruitMember(debabrata);
    itMediator.recruitMember(liton);

    saikat.assignWork("Java Work");
    System.out.println("--------------------------------");
    sumit.assignWork("Mule Work");

  }

}

Run the above test class you will see the below output.

Output

Saikat assigning task : Java Work
Sumit got a task : Java Work
Soumen got a task : Java Work
Gourab got a task : Java Work
Debmalya got a task : Java Work
Debabrata got a task : Java Work
Liton got a task : Java Work
--------------------------------
Sumit assigning task : Mule Work
Saikat got a task : Mule Work
Soumen got a task : Mule Work
Gourab got a task : Mule Work
Debmalya got a task : Mule Work
Debabrata got a task : Mule Work
Liton got a task : Mule Work

That’s all. Thanks for your reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *