ATC — An Object Oriented Design

Today, I was woken up by a thunderous sound. This is not unusual as I live close to the airport. As I tried to go back to sleep, I thought to myself, would it be such a problem if all flights started operations after noon? Wouldn’t be nice to have this control over them?
And so, I got my “real world thing” for today.
Air Traffic Control
Air traffic control (ATC) is a service provided by ground-based air traffic controllers who direct aircraft on the ground and through controlled airspace, and can provide advisory services to aircraft in non-controlled airspace.
Without ATC, communication between flights(pilots) would look something like this.

Without the air traffic controller, pilots would need to be aware of every plane in the vicinity of the airport, discussing landing priorities with dozens of other pilots.
Each flight will need to interact with every other flight before executing any action.
# Flights in air
qa_flight = QatarAirways.new
sa_flight = SingaporeAirlines.new
ana_flight = AllNipponAirways.new
cpa_flight = CathayPacificAirways.new
# ...# All flights must be aware of each other
qa_flight.acknowledge(sa_flight, ana_flight, cpa_flight)
sa_flight.acknowledge(qa_flight, ana_flight, cpa_flight)
ana_flight.acknowledge(qa_flight, sa_flight, cpa_flight)
cpa_flight.acknowledge(qa_flight, sa_flight, ana_flight)
# ...# In order to land, Qatar Airways flight A must run certain checks
# such as it's priority compared to other flights, checking runway availability, etc
qa_flight.can_land?# Other flights need to run the same checks
sa_flight.can_land?
ana_flight.can_land?
cpa_flight.can_land?
# ...
In case of heavy air traffic (large number of flights), the situation will become virtually impossible to handle and probably lead to a steep rise in airplane crash statistics.
ATC’s function is to prevent the situation from becoming so grim.
Let’s get crackin’
Different Aircrafts and one AirTrafficController are the main entities involved here.
ATC, by definition, acts as a mediator between flights. Direct communication between flights is not allowed. All communication happens via ATC.
Based on this behaviour of this system, we can incorporate the Mediator Design Pattern
Mediator Design Pattern
Mediator is a behavioural design pattern that lets you reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object.

- Components are classes with business logic and a reference to a mediator.
- Mediator interface declares methods of communication with components.
- Concrete Mediators encapsulate relations between various components. and keep references to all components they manage
- Components must be decoupled and only notify the mediator.
Classes and Objects
In simple words, with the Mediator pattern in place, the flights become independent component objects which require collaboration and the ATC serves as a single mediator class that redirects the calls to appropriate components and removes coupling between them
So classes for our design will be:
- Mediator
- AirTrafficController
- Aircraft
- Components classes for flights — QatarAirways, SingaporeAirlines, AllNipponAirways, CathayPacificAirways

The Code
An interface for the mediator class:
AirTrafficController serves as the concrete mediator which handles landing of flights
Aircraft serves as an abstract class to create different component objects — flights that need to land.
This is how this system will function
atc = AirTrafficController.newqa_flight = QatarAirways.new('QA2341', 4000, atc)
sa_flight = SingaporeAirlines.new('SA9354', 2000, atc)
ana_flight = AllNipponAirways.new('AN2344', 1000, atc)
cpa_flight = CathayPacificAirways.new('CP2976', 3000, atc)sa_flight.land
# => Permission deniedana_flight.land
# Landing initiated