Design Pattern – Mediator Pattern

Tram Ho

Mediator Pattern

Visitor Pattern is one of the Behavior Pattern group – is a highly applicable pattern that we have encountered quite a lot in programming in general and iOS programming in particular.

Content: Still the same as the previous article we will follow 3 main parts:

  • Overview of Mediator Pattern
  • How to use Mediator Pattern
  • Application of Mediator Pattern in IOS programming

Overview of Mediator Pattern

In general and object-oriented programming in particular, the distribution or behavior display between objects is always encouraged. This leads to a certain circumstances that can lead to all objects know about each other to form a close relationship that is difficult to separate.

The Mediator pattern was born to reduce the complexity of communication between classes and objects. Provides an intermediary layer that processes communication between layers, facilitates code maintenance easier with loose couplings – i.e., similar objects do not communicate with each other. will pass through an intermediate object and allow you to change the way of interaction between them independently, this will avoid tight coupling of components in the system.

The components of the mediator pattern

  • Colleague: a protocol that holds reference to the Mediator object.
  • ConcreteColleague: install Colleague methods. Communicate via Mediator when needed to communicate with other Colleague.
  • Mediator: is a protocol that defines methods for communicating with Colleague objects.
  • ConcreteMediator: Install Mediator methods, know and manage Colleague objects.

How to use Mediator Pattern

Let’s go into a specific example.

Normally, the activities in a project that we often see will be as follows: It looks very complicated and closely linked, it’s hard to change personnel and work quite overlapping, right?

And this is the model after applying Mediator All communication will be via Mediator.

Let’s write this example together:

First, create the Mediator protocol, which contains the send method to communicate between members.

Next we will create TeamMediator to implement Mediator and manage all members of the project. There will be activities of adding other members as well as communication activities from the members.

Next we go to create members of the project

All members know the mediator and have methods of communication such as sending and receiving information. So we have initialized the components of the Mediator pattern . Let’s try it together. Initiate members and teamMediator then register members for the team, each member will send greetings to all.

Pretty simple, isn’t it?

Application of Mediator Pattern in IOS programming

Probably programmers are quite familiar with the following image.

This is the Viewcontrollers in UIKit

IOS applications often use the poetic and ostentatious user interface design to present the screen of the application data model. In this image, the UIViewController is an abstract class that can be layered to manage tool bar and navigation bar objects. Inside the UIViewController are UiViews like buttons , labels, etc. that can interact and push the opposite effect on other components through the UIViewController. That is Mediator.

Hopefully the article will solve your questions about Mediator


Chia sẻ bài viết ngay

Nguồn bài viết : Viblo