Saturday, November 13, 2010

..........Mediator Pattern

Mediator pattern, used to handle complex communications between related objects, helping with decoupling of those objects.
Example :-
An airport control tower is an excellent example of the mediator pattern. The tower looks after who can take off and land - all communications are done from the airplane to control tower, rather than having plane-to-plane communication. This idea of a central controller is one of the key aspects to the mediator pattern.

private class AirPlane:ControllRoom
{
private String m_sAirPlaneNumber =null;

public AirPlane(String sAirPlaneNumber)
{
m_sAirPlaneNumber = sAirPlaneNumber;
}

public Register(Airplane objAirplane)
{
m_arlsAirplanes.Add(objAirplane);
}
}


private class ControllRoom
{
private Arraylist m_arlsAirplanes =null;
public Arraylist Airplanes
{
get
{
return m_arlsAirplanes;
}
}
public ControllRoom()
{
m_arlstAirplanes = new Arraylist();
}

private Arraylist m_arlstOnAirport =null;

public Arraylist OnAirport
{
get
{
return m_arlstOnAirPort;
}
}

public void AddOnAirPort(Airplane objAirplane)
{
m_arlstOnAirPort.Add(objAirplane);
}
public void RemoveFromAirPort(Airplane objAirplane)
{
m_arlstOnAirPort.Remove(objAirplane);
}

}
static void main()
{
Airplane objAirplaneA = new Airplane(“A100”);
objAirplaneA.Register(objAirplane);

if(objAirplaneA.OnAirport.Count <= 1)
objAirPlaneA.AddOnAirport(objAirplaneA);


Airplane objAirplaneB = new Airplane(“B100”);
objAirplaneB.Register(objAirplaneB);

if(objAirplaneB.OnAirport.Count <= 1)
objAirPlaneB.AddOnAirport(objAirplaneB);
}
}

No comments:

Followers

Link