The Mediator Design Pattern in Swift

Loose coupling!

Image for post
Image for post
Photo by Jason Rosewell

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.

Image for post
Image for post

You could probably set up an algorithm about which planes the control tower would communicate with at any given time to do with the distance of any given plane from the others.

The Mediator Design Pattern in Swift

The following mediator does not have any special logic around which of the receivers that would be sent a request.

However, far from being entirely theoretical this is the design pattern I have used in my implementation of two-way binding.

The code is in the playground in the repo, and I hope you download that and enjoy seeing it work in front of your eyes!

Enhancing behaviour

Although the Mediator Pattern is known as a common design pattern, it really deals with the communication between objects, and does this by providing a unified interface to a set of subsystem interfaces.

This is known as a behavioral pattern, due to this inclination towards communication.

Once implemented, the Mediator pattern is usually considered to be a good design pattern but care must be taken to make sure that the design does not lead itself to being a god object.

Conclusion

The Mediator design pattern is rather about encapsulating the communication between objects.

It is often a good idea to use the mediator pattern to decouple classes due to their communication.

Reduce tight coupling? This is certainly something that you should be thinking about doing, and considering how your application of OO can be improved over time.

If you’ve any questions, comments or suggestions please hit me up on Twitter

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store