Member-only story
The Mediator Design Pattern in Swift
Loose coupling!
Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 12.1, and Swift 5.3
Prerequisites:
Mediator Pattern: the definition of an object that encapsulates how objects interact Design Pattern: a general, reusable solution to a commonly occurring problem
Encapsulation: the bundling of data with the methods that operate on that data, or the restriction of direct access to some of an object’s components
The problem
When two (or more!) objects communicate, we need to be mindful that if we use the adaptor pattern we may be tightly coupling objects — and it goes without saying that this isn’t ideal.
One cannonical example of the mediator pattern is an aircraft control tower communicating with planes within the tower’s range. The planes are required to be a certain distance apart (and certainly not crash!) but do not communicate directly with each other! In this case, they relay their position to the control tower which then communicates the relevant information to planes.