Mediator - SupplementaryExamples
Mediator - SupplementaryExamples
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.)
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
}
_aircraftUnderGuidance.Add(aircraft);
?
} 7
}
After Applying Design Pattern (cont.)
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)
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.)
14
After Applying Design Pattern (cont.)
mediator.addColleague(desktop);
mediator.addColleague(mobile);
desktop.send("Hello World");
mobile.send("Hello");
}
}
15
Example 3:
Software Development Team
Problem
17
Before Applying Design Pattern
project project
manager manager
developer developer
quality quality
engineer engineer
18
After Applying Design Pattern
project project
manager manager
quality quality
engineer
? engineer
19