Behavioral – Mediator Design Pattern
August 10, 2020 1 min read

Mediator pattern is one of the important behavioral design pattern. According to GoF, mediator pattern allows loose coupling by encapsulating the way disparate sets of objects interact and communicate with each other. Allows for the actions of each object set to vary independently of one another.
Sport car race controller is a example of mediator pattern where the race control room works as a mediator for communication between different cars. Mediator works as a router between objects and it can have its own logic to provide way of communication.
Let’s see the example of mediator pattern.

“IRaceMediator” interface defines communication between colleague objects. You can see some classes are needed to be created for this interface. Let’s see what are those, from the next steps.

This is the concrete mediator class that implements the mediator interface “IRaceMediator” and coordinates communication between colleague objects.

“Command” is the colleague interface that communicate with other colleagues. “start()” method is the common command for the below given classes.

“Car” and “Track” are concrete colleague classes implemented by the colleague interface “Command”. These classes communicate each other through their mediator. Simply, communication between car and the track is going through the control room. Let’s see the main method.

Output of the above program is:

You can visit the below given articles to learn about creational design patterns and Structural Design Patterns.
Thank you.
Creational Design Patterns
Abstract Factory Design Pattern
Structural Design Patterns


