ATC — An Object Oriented Design. Today, I was woken up by a thunderous…
By: Nitish Sharma
Tags: Mediator Pattern, Object Oriented, Solid Principles, Air Traffic Control

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.

Flights managing chaotically without ATC

With­out the air traf­fic con­troller, pilots would need to be aware of every plane in the vicin­i­ty of the air­port, dis­cussing land­ing pri­or­i­ties 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

Medi­a­tor is a behav­ioural design pat­tern that lets you reduce chaot­ic depen­den­cies between objects. The pat­tern restricts direct com­mu­ni­ca­tions between the objects and forces them to col­lab­o­rate only via a medi­a­tor object.

Mediator pattern UML diagram

Com­po­nents are class­es with busi­ness logic and a ref­er­ence to a medi­a­tor.

are class­es with busi­ness logic and a ref­er­ence to a medi­a­tor. Medi­a­tor inter­face declares meth­ods of com­mu­ni­ca­tion with com­po­nents.

declares meth­ods of com­mu­ni­ca­tion with com­po­nents. Con­crete Medi­a­tors encap­su­late rela­tions between var­i­ous com­po­nents. and keep ref­er­ences to all com­po­nents they man­age

encap­su­late rela­tions between var­i­ous com­po­nents. and keep ref­er­ences to all com­po­nents they man­age Com­po­nents must be decoupled and only noti­fy the medi­a­tor.

Classes and Objects

In simple words, with the Medi­a­tor pat­tern in place, the flights become independent component objects which require collaboration and the ATC serves as a single mediator class that redi­rects the calls to appro­pri­ate com­po­nents and removes coupling between them

So classes for our design will be:

Mediator

AirTrafficController

Aircraft

Components classes for flights — QatarAirways, SingaporeAirlines, AllNipponAirways, CathayPacificAirways

Class diagram for the system

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.new qa_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 denied ana_flight.land

# Landing initiated

References