Mediator Pattern
Mediator is a behavioral design pattern that reduces coupling between components of a program by making them communicate indirectly, through a special mediator object.
The Mediator makes it easy to modify, extend and reuse individual components because they’re no longer dependent on the dozens of other classes.
Golang Implementation
Example: Railway Station Traffic System
An excellent example of the Mediator pattern is a railway station traffic system. Two trains never communicate between themselves for the availability of the platform. The stationManager acts as a mediator and makes the platform available to only one of the arriving trains while keeping the rest in a queue. A departing train notifies the stations, which lets the next train in the queue to arrive.
train.go
package main
type Train interface {
arrive()
depart()
permitArrival()
}
passengerTrain.go
package main
import "fmt"
type PassengerTrain struct {
mediator Mediator
}
func (g *PassengerTrain) arrive() {
if !g.mediator.canArrive(g) {
fmt.Println("PassengerTrain: Arrival blocked, waiting")
return
}
fmt.Println("PassengerTrain: Arrived")
}
func (g *PassengerTrain) depart() {
fmt.Println("PassengerTrain: Leaving")
g.mediator.notifyAboutDeparture()
}
func (g *PassengerTrain) permitArrival() {
fmt.Println("PassengerTrain: Arrival permitted, arriving")
g.arrive()
}
freightTrain.go
package main
import "fmt"
type FreightTrain struct {
mediator Mediator
}
func (g *FreightTrain) arrive() {
if !g.mediator.canArrive(g) {
fmt.Println("FreightTrain: Arrival blocked, waiting")
return
}
fmt.Println("FreightTrain: Arrived")
}
func (g *FreightTrain) depart() {
fmt.Println("FreightTrain: Leaving")
g.mediator.notifyAboutDeparture()
}
func (g *FreightTrain) permitArrival() {
fmt.Println("FreightTrain: Arrival permitted")
g.arrive()
}
mediator.go
package main
type Mediator interface {
canArrive(Train) bool
notifyAboutDeparture()
}
stationManager.go
package main
type StationManager struct {
isPlatformFree bool
trainQueue []Train
}
func newStationManger() *StationManager {
return &StationManager{
isPlatformFree: true,
}
}
func (s *StationManager) canArrive(t Train) bool {
if s.isPlatformFree {
s.isPlatformFree = false
return true
}
s.trainQueue = append(s.trainQueue, t)
return false
}
func (s *StationManager) notifyAboutDeparture() {
if !s.isPlatformFree {
s.isPlatformFree = true
}
if len(s.trainQueue) > 0 {
firstTrainInQueue := s.trainQueue[0]
s.trainQueue = s.trainQueue[1:]
firstTrainInQueue.permitArrival()
}
}
main.go
package main
func main() {
stationManager := newStationManger()
passengerTrain := &PassengerTrain{
mediator: stationManager,
}
freightTrain := &FreightTrain{
mediator: stationManager,
}
passengerTrain.arrive()
freightTrain.arrive()
passengerTrain.depart()
}
// PassengerTrain: Arrived
// FreightTrain: Arrival blocked, waiting
// PassengerTrain: Leaving
// FreightTrain: Arrival permitted
// FreightTrain: Arrived
Rust Implementation
Example: Railway Station Traffic System
Top-Down Ownership approach allows to apply Mediator in Rust as it is a suitable for Rust’s ownership model with strict borrow checker rules. It’s not the only way to implement Mediator, but it’s a fundamental one.
The key point is thinking in terms of OWNERSHIP.
- A mediator takes ownership of all components.
- A component doesn’t preserve a reference to a mediator. Instead, it gets the reference via a method call.
- Control flow starts from fn main() where the mediator receives external events/commands.
- Mediator trait for the interaction between components (notify_about_arrival, notify_about_departure) is not the same as its external API for receiving external events (accept, depart commands from the main loop).
train_station.rs
use std::collections::{HashMap, VecDeque};
use crate::trains::Train;
// Mediator has notification methods.
pub trait Mediator {
fn notify_about_arrival(&mut self, train_name: &str) -> bool;
fn notify_about_departure(&mut self, train_name: &str);
}
#[derive(Default)]
pub struct TrainStation {
trains: HashMap<String, Box<dyn Train>>,
train_queue: VecDeque<String>,
train_on_platform: Option<String>,
}
impl Mediator for TrainStation {
fn notify_about_arrival(&mut self, train_name: &str) -> bool {
if self.train_on_platform.is_some() {
self.train_queue.push_back(train_name.into());
false
} else {
self.train_on_platform.replace(train_name.into());
true
}
}
fn notify_about_departure(&mut self, train_name: &str) {
if Some(train_name.into()) == self.train_on_platform {
self.train_on_platform = None;
if let Some(next_train_name) = self.train_queue.pop_front() {
let mut next_train = self.trains.remove(&next_train_name).unwrap();
next_train.arrive(self);
self.trains.insert(next_train_name.clone(), next_train);
self.train_on_platform = Some(next_train_name);
}
}
}
}
impl TrainStation {
pub fn accept(&mut self, mut train: impl Train + 'static) {
if self.trains.contains_key(train.name()) {
println!("{} has already arrived", train.name());
return;
}
train.arrive(self);
self.trains.insert(train.name().clone(), Box::new(train));
}
pub fn depart(&mut self, name: &'static str) {
let train = self.trains.remove(name);
if let Some(mut train) = train {
train.depart(self);
} else {
println!("'{}' is not on the station!", name);
}
}
}
trains/mod.rs
mod freight_train;
mod passenger_train;
pub use freight_train::FreightTrain;
pub use passenger_train::PassengerTrain;
use crate::train_station::Mediator;
// A train gets a mediator object by reference.
pub trait Train {
fn name(&self) -> &String;
fn arrive(&mut self, mediator: &mut dyn Mediator);
fn depart(&mut self, mediator: &mut dyn Mediator);
}
trains/freight_train.rs
use super::Train;
use crate::train_station::Mediator;
pub struct FreightTrain {
name: String,
}
impl FreightTrain {
pub fn new(name: &'static str) -> Self {
Self { name: name.into() }
}
}
impl Train for FreightTrain {
fn name(&self) -> &String {
&self.name
}
fn arrive(&mut self, mediator: &mut dyn Mediator) {
if !mediator.notify_about_arrival(&self.name) {
println!("Freight train {}: Arrival blocked, waiting", self.name);
return;
}
println!("Freight train {}: Arrived", self.name);
}
fn depart(&mut self, mediator: &mut dyn Mediator) {
println!("Freight train {}: Leaving", self.name);
mediator.notify_about_departure(&self.name);
}
}
trains/passenger_train.rs
use super::Train;
use crate::train_station::Mediator;
pub struct PassengerTrain {
name: String,
}
impl PassengerTrain {
pub fn new(name: &'static str) -> Self {
Self { name: name.into() }
}
}
impl Train for PassengerTrain {
fn name(&self) -> &String {
&self.name
}
fn arrive(&mut self, mediator: &mut dyn Mediator) {
if !mediator.notify_about_arrival(&self.name) {
println!("Passenger train {}: Arrival blocked, waiting", self.name);
return;
}
println!("Passenger train {}: Arrived", self.name);
}
fn depart(&mut self, mediator: &mut dyn Mediator) {
println!("Passenger train {}: Leaving", self.name);
mediator.notify_about_departure(&self.name);
}
}
main.rs
mod train_station;
mod trains;
use train_station::TrainStation;
use trains::{FreightTrain, PassengerTrain};
fn main() {
let train1 = PassengerTrain::new("Train 1");
let train2 = FreightTrain::new("Train 2");
// Station has `accept` and `depart` methods,
// but it also implements `Mediator`.
let mut station = TrainStation::default();
// Station is taking ownership of the trains.
station.accept(train1);
station.accept(train2);
// `train1` and `train2` have been moved inside,
// but we can use train names to depart them.
station.depart("Train 1");
station.depart("Train 2");
station.depart("Train 3");
}
// Passenger train Train 1: Arrived
// Freight train Train 2: Arrival blocked, waiting
// Passenger train Train 1: Leaving
// Freight train Train 2: Arrived
// Freight train Train 2: Leaving
// 'Train 3' is not on the station!