0% found this document useful (0 votes)
46 views20 pages

Mediator - SupplementaryExamples

The document describes the mediator design pattern. It provides examples of how the pattern can be used to solve communication problems between different types of objects. In one example, an aircraft traffic controller acts as a mediator to allow different types of aircraft to communicate safely. Another example shows how a chatroom mediator allows participants to communicate by sending messages to chatrooms. A third example shows how a mediator can notify different roles on a software team when new code is committed. The mediator pattern allows communication between classes or objects to be decoupled by having a mediator object manage their interactions.

Uploaded by

amirreza alasti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views20 pages

Mediator - SupplementaryExamples

The document describes the mediator design pattern. It provides examples of how the pattern can be used to solve communication problems between different types of objects. In one example, an aircraft traffic controller acts as a mediator to allow different types of aircraft to communicate safely. Another example shows how a chatroom mediator allows participants to communicate by sending messages to chatrooms. A third example shows how a mediator can notify different roles on a software team when new code is committed. The mediator pattern allows communication between classes or objects to be decoupled by having a mediator object manage their interactions.

Uploaded by

amirreza alasti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Mediator Design Pattern

Dr. Abbas Rasoolzadegan


Example 1: Aircraft Controller
Problem

 Consider a scenario where incoming aircraft need to carefully


communicate with each other for safety reasons. They constantly
need to know the position of all other planes, meaning that each
aircraft needs to communicate with all other aircraft. You have 3
types of aircraft in your domain model: Boeing, Airbus and
Fokker. Consider that each type needs to communicate with the
other two types.

2
After Applying Design Pattern (in C#)
public abstract class Aircraft {
private readonly ITrafficControl _atc;
private int _currentAltitude;
protected Aircraft(string callSign, ITrafficControl atc) {
_atc = atc;

Abstract Colleague
CallSign = callSign;
_atc.RegisterAircraftUnderGuidanc(this);
}
public abstract int Ceiling { get; }
public string CallSign { get; private set; }
public int Altitude {
get { return _currentAltitude; }
set { _currentAltitude = value;
_atc.ReceiveAircraftLocation(this);
}
3
}
After Applying Design Pattern (cont.)
public void Climb(int heightToClimb) {
Altitude += heightToClimb;
}
public void Warn(Aircraft reportingAircraft){ Abstract Colleague (cont.)
//do something in response to the warning
}
}

4
After Applying Design Pattern (cont.)

public interface ITrafficControl {


void ReceiveAircraftLocation(Aircraft aircraft); Mediator
Interface
void RegisterAircraftUnderGuidance(Aircraft aircraft);
}

5
After Applying Design Pattern (cont.)
public class Boeing : Aircraft {
public Boeing(string callSign, ITrafficControl atc): base(callSign, atc) { }
public override int Ceiling {
get { return 33000; }}
}
Concrete ColleagueS

public class Fokker : Aircraft {


public Fokker(string callSign, ITrafficControl atc) : base(callSign, atc) { }
public override int Ceiling {
get { return 40000; }}
}
public class Airbus : Aircraft {
public Airbus(string callSign, ITrafficControl atc): base(callSign, atc) { }
public override int Ceiling {
get { return 40000; }}
}
6
After Applying Design Pattern (cont.)
public class Tower : ITrafficControl {
private readonly IList<Aircraft> _aircraftUnderGuidance = new List<Aircraft>();
public void ReceiveAircraftLocation(Aircraft reportingAircraft) {
foreach (Aircraft currentAircraftUnderGuidance in _aircraftUnderGuidance.
Where(x => x != reportingAircraft)) {
if (Math.Abs(currentAircraftUnderGuidance.Altitude-
Concrete Mediator

reportingAircraft.Altitude) < 1000) {


reportingAircraft.Climb(1000);
//communicate to the class
currentAircraftUnderGuidance.Warn(reportingAircraft);
}
}
}
public void RegisterAircraftUnderGuidance(Aircraft aircraft) {
if (!_aircraftUnderGuidance.Contains(aircraft)) {

}
_aircraftUnderGuidance.Add(aircraft);
?
} 7
}
After Applying Design Pattern (cont.)

public class Client {


static void main(String[] args) {
ITrafficControl tower = new Tower();
Aircraft flight1 = new Airbus("AC159", tower);
Aircraft flight2 = new Boeing("WS203", tower); Client
Aircraft flight3 = new Fokker("AC602", tower); Class
flight1.Altitude += 1000;
}

8
Example 2: Chatroom
Problem

 You can think of the mediator as a chat room, each participant can
register to different chat rooms and send messages to the chat
rooms. Only the participants in the chat room will receive the
message.
 An observer based variation of the mediator pattern is used in Java
Message Service (JMS) implementations, which allows
applications to subscribe and publish data to other applications.
This is a common combination of patterns that makes sense.

10
After Applying Design Pattern (in Java)

public interface Mediator


{ Mediator
public void send(String message, Colleague colleague); Interface
}

11
After Applying Design Pattern (cont.)
public abstract Colleague{
private Mediator mediator;
public Colleague(Mediator m) {
mediator = m;
}
//send a message via the mediator
public void send(String message) {
mediator.send(message, this); Abstract Colleague
}
//get access to the mediator
public Mediator getMediator() {
return mediator;
}
public abstract void receive(String message);
} 12
After Applying Design Pattern (cont.)
public class ApplicationMediator implements Mediator {
private ArrayList<Colleague> colleagues;
public ApplicationMediator() {
colleagues = new ArrayList<Colleague>();
}
public void addColleague(Colleague colleague) {
colleagues.add(colleague);
}
Concrete
public void send(String message, Colleague originator) {
Mediator
//let all other screens know that this screen has changed
for (Colleague colleague: colleagues) {
//don't tell ourselves
if (colleague != originator)
colleage.receive(message);
}
}
} 13
After Applying Design Pattern (cont.)

public class ConcreteColleague extends Colleague {

public void receive(String message) {


System.out.println("Colleague Received: " + message); Concrete
Colleague1
}

public class MobileColleague extends Colleague {


public void receive(String message) {

System.out.println("Mobile Received: " + message); Concrete


Colleague2
}

14
After Applying Design Pattern (cont.)

public class Client {

public static void main(String[] args) {

ApplicationMediator mediator = new ApplicationMediator();

ConcreteColleague desktop = new ConcreteColleague(mediator);


Client Class

ConcreteColleague mobile = new MobileColleague(mediator);

mediator.addColleague(desktop);

mediator.addColleague(mobile);

desktop.send("Hello World");

mobile.send("Hello");

}
}

15
Example 3:
Software Development Team
Problem

 Consider in a software development team, there are project


manager, developer and quality engineer. When the developer
completes coding for a new feature, the codes are committed to the
repository. Other shareholders like quality engineer and project
manager need to be notified…

17
Before Applying Design Pattern

 Every role needs to hold instances of other roles. The connection is so


tight that any changes are not easy to make.

project project
manager manager

developer developer

quality quality
engineer engineer
18
After Applying Design Pattern

 After applying Mediator pattern the structure becomes to the following:

project project
manager manager

developer Mediator developer

quality quality
engineer
? engineer

19

You might also like