Thursday, January 19, 2012

Going Technical....Mediator Pattern

Have you ever attempted to book a cab ? If yes then today's lesson is going to be easy for you. For the people who have not ever done that , I am going to give a crash course :). Well if you need to book a cab you just need to call the cab service provider's number and talk to the representative at the other end.

When you inform the representative about the pick up point and the destination he or she will do the coordination and pass this information to a cab which is nearer to your location. In this case you talked to the representative not to the cab driver. And the cab driver also talked to the representative , not to you directly. In short representative is becoming a medium for you to transfer the message that "you need a cab" , and the representative transfers a message to cab driver saying that "there is a passenger waiting for".

Imagine that if you ever had to book a cab by directly calling a cab driver, it would have been extremely difficult and chaotic. On the other hand you would have to know about all the numbers of cab drivers whom you could probably contact. But surely , we know that this is not going to work easy for us and we will be wasting time trying to get hold of a cab. Instead you get a representative who has all the information about the cab drivers such as contact numbers , current location etc.

Well that is the real life example , but how can we apply this in the field of software ? . Imagine for a moment that you have two or more applications which need to send messages among each other , possibly to do some application processing. Let's say the systems are SystemA , SystemB and SystemC. If these systems send messages to each other , it would be like,

1. From SystemA to SystemB
2. From SystemB to SystemA
3. From SystemB to SystemC
4. From SystemC to SystemB
5. From SystemC to SystemA
6. From SystemA to SystemC

If System A , System B , System C communicates as mentioned above , those will surely be making a spaghetti when communications are considered among them. It is chaotic , it is difficult to maintain and would waste lot of time and energy.

Now that , lets say we are going to introduce some other entity as a middleman among these systems and mandate those system to communicate to the middleman at the same time mandating the middleman to communicate to the systems whenever it is needed. Once this arrangement is in place , the middle man becomes a focal point. System A , System B and System C communicate to middleman as a client and middleman would communicate to any system which is acting as a receiver according to the request from a client.

In this case , systems do not communicate to each other directly , instead a mediator is used. The mediator would know to which system to send the messages received from the client. This set up will surely make it easy to maintain , would help specializing roles of the systems and would surely save some time and effort as well.

That is what we call as Mediator pattern , it is quite simple. Let's do some coding then....



 public abstract class System{
  private MiddleMan middleMan;
  public void registerMiddleman(MiddleMan middleMan){
   this.middleMan = middleMan
  }
  public void sendExternalSystemMessage(String message , System system){
   middleMan.recordMessageFor(message,system);
  }
  public void receiveExternalMessage(String message , System receivedFrom){
   //do something with the message from another system
  }
 }


//defining systems

 public class SystemA extends System{
 }
 public class SystemB extends System{
 }
 public class SystemC extends System{
 }


//defining Middleman

 public class MiddleMan{

  private ArrayList<System> systems = new ArrayList<System>();
  private Map<System,String> messageMap = new HashMap<System,String>();
  public void recordMessageFor(String message,System system){
   messageMap.put(system,message);
  }
  public void registerSystem(System system){
   systems.add(system);
  }
  public void sendMessages(){
   Set<System> keys = messageMap.keySet();
   Iterator<System> _it = keys.iterator();
   while(_it.hasNext()){
    System system = _it.next();
    message = messageMap.get(system);
    system.receiveExternalMessage(message,system);
   }
  }
 }

//A main method in another class

 public static void main(String[] args){

   System systemA = new SystemA();
   System systemB = new SystemB();
   System systemC = new SystemC();

   MiddleMan middleMan = new MiddleMan();

   //register middle man with each system,
   //this is as if all the cab drivers know the representative
   systemA.registerMiddleman(middleMan);
   systemB.registerMiddleman(middleMan);
   systemC.registerMiddleman(middleMan);

   //register systems with middleman, this is as if representative knows
   //the all the cab drivers
   middleMan.registerSystem(systemA);
   middleMan.registerSystem(systemB);
   middleMan.registerSystem(systemC);

   //send messages from systems to systems
   systemA.sendExternalSystemMessage("this is from A to B",systemB);
   systemB.sendExternalSystemMessage("this is from B to C",systemC);
   ......

   //middleman does the coordination...
   middleMan.sendMessages();
 }



No comments: