Mediator Pattern - Swift

Abhishek Kumar
Aug 31, 2018 · 1 min read

Let’s take an example first for understanding the problem (why this is needed?)

There were 3 friends in a school : Tom, Gary and Ramu

Three of them decided to track one another activities.

If Tom has finished a chapter of history Gary and Ramu like to know about it. Similarly when Gary has done with a chapter in history Tom and Ramu want to know about it.

Now If we have to design this and make an iOS app which serve above purpose how we gonna do this:

We will have 3 class Tom, Gary and Ramu and each class have reference to other 2 class.

Every class will have both send and receive method for this purpose.

This system is called as tightly coupled system.

Tightly Coupled System where change in one object requires to call all other observers send method explicilty.

Now, In order to solve these kind of problems mediator pattern is used where everyone communicates with mediator and then mediator take care of the rest.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade