Behavioral_Mediator
β Mediator
- mediator object control how the objects interact
- objects communicate through mediator
ππ» before: objects communicate directly with each other, tight coupling
- ππ» after
- components only know the mediator, not each other
1
2
3
Component A \
Component B ---> Mediator ---> Other components
Component C /
β Diagram
π Send message
βοΈ Mediator Interface
1
2
3
interface Mediator {
void sendMessage(String message, Colleague colleague);
}
βοΈ Concrete Mediator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ChatMediator implements Mediator {
private User1 user1; //should have concrete colleague as field
private User2 user2;
public void setUser1(User1 user1) {
this.user1 = user1;
}
public void setUser2(User2 user2) {
this.user2 = user2;
}
@Override
public void sendMessage(String message, Colleague colleague) {
if (colleague == user1) {
user2.receive(message);
} else {
user1.receive(message);
}
}
}
βοΈ Abstract Colleague class(component)
- colleague does not have to know other colleagues
- only need to know the mediator
1
2
3
4
5
6
7
abstract class Colleague {
protected Mediator mediator; //now colleague knows only mediator
public Colleague(Mediator mediator) {
this.mediator = mediator;
}
}
- objects that communicate through the mediator
βοΈ Concrete Colleage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class User1 extends Colleague {
public User1(Mediator mediator) {
super(mediator);
}
public void send(String message) {
mediator.sendMessage(message, this);
}
public void receive(String message) {
System.out.println("User1 received: " + message);
}
}
//also user2, user3, user4...
//they all communicate via Mediator
βοΈ Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Main {
public static void main(String[] args) {
ChatMediator mediator = new ChatMediator();
User1 u1 = new User1(mediator);
User2 u2 = new User2(mediator);
mediator.setUser1(u1);
mediator.setUser2(u2);
u1.send("Hello from User1");
u2.send("Hello from User2");
}
}
//User2 received: Hello from User1
//User1 received: Hello from User2
π Air Traffic control
- flights do not communicate with each other directly
- they talk to the control tower
- control tower mediates the communicatation among flights
βοΈ Mediator Interface
- define how to communicate
1
2
3
4
interface AirportMediator {
boolean isRunwayAvailable(); //check if runway is available
void setRunwayAvailability(boolean status); //change runway availability
}
βοΈ Concrete Mediator
- implement the coordination logic
1
2
3
4
5
6
7
8
9
10
11
12
class AirportControlTower implements AirportMediator {
private boolean isRunwayAvailable = true;
//override
public boolean isRunwayAvailable() {
return isRunwayAvailable;
}
//override
public void setRunwayAvailability(boolean status) {
isRunwayAvailable = status;
}
}
βοΈ Concrete Colleage
- objects that communicate through the mediator
- actual components
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Flight {
private AirportMediator mediator;
private String flightNumber;
public void land() {
//check if runway is available and land
}
}
class Runway {
private AirportMediator mediator;
public void clear() {
//make runway available again
}
}
βοΈ Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class AirportSystem {
public static void main(String[] args) {
AirportMediator controlTower = new AirportControlTower(); //use airportControlerMediator
Flight flight1 = new Flight(controlTower, "KE123");
Flight flight2 = new Flight(controlTower, "OZ456");
Runway runway = new Runway(controlTower);
flight1.land();
flight2.land();
runway.clear();
flight2.land();
}
}
π οΈ
- when order is created, talk to email, inventory, analytics
1
2
3
OrderService ----> EventPublisher (Mediator) ----> EmailService
----> InventoryService
----> AnalyticsService
ππ»
- lose coupling
- easier maintenance
This post is licensed under CC BY 4.0 by the author.

