Introduction to the

Within a project, a program consists of a large number of classes. As functionality increases, the dependencies between classes become more complex, and the mediator pattern solves this problem. The mediator mode is an object that encapsulates the interaction of a group or a series of objects, so that the interaction between objects can be handled in a mediator object, so that the objects are loosely coupled and the interaction between them can be changed independently.

An intermediary is like a traffic policeman at an intersection. If you change the traffic pattern at the intersection, you just need to give the new traffic strategy to the traffic police, not all the cars on the road, so that you can better coordinate the traffic coming from different directions.

benefit

Loose coupling

By encapsulating the interactions between multiple peer objects into the mediation object, the mediator pattern enables the simultaneous objects to be loosely coupled and essentially independent of each other. In this way, objects can be changed and reused independently, rather than “pulling the strings”.

2. Centralized control interaction

Interactions between multiple co-worker objects are encapsulated in a centralized intermediary object, so that when these interactions change, only the mediator needs to be modified.

Many-to-many becomes many-to-many

Without the intermediary pattern, the relationship between the co-worker objects is usually many-to-many. With the introduction of the intermediary object, the relationship between the mediator and the co-worker objects is usually bidirectional one-to-many, which makes the relationship between the objects easier to understand and implement.

The model structure

1. Mediator: A Mediator that defines the methods needed to interact with colleagues, whether public or small-scale. ConcreteMeditor: A ConcreteMeditor that understands and maintains peer objects and is responsible for coordinating interactions between peer objects. The Colleague class mainly restricts the type of the Colleague object and implements some common functionality between the concrete Colleague classes. For example, each concrete Colleague class should know about the intermediary object, that is, the concrete Colleague class holds the intermediary object and can be used in the class. ConcreteColleague: The ConcreteColleague class implements its business, and when it needs to communicate with other colleagues, it communicates with its own intermediaries, who are responsible for interacting with other colleagues.

Typical Application Scenarios

  • C(Controller) in MVC is an intermediary. Its role is to separate M and V, coordinate the work of M and V, and fuse the result of M running and the view represented by V into a page that can be displayed in front, so as to reduce the dependency between M and V.

  • The UINavigationController in iOS is the intermediary that manages the jump to the view. In general, a page corresponds to a VC in the code, and a medium size software will have at least dozens of pages, corresponding to dozens of VC. Managing the relationship between these VC’s can be very tedious. Every time a VC wants to jump to another VC, we need to include a new VC’s header file, so some of the linking VC’s contain a large number of other VC’s headers. Using the intermediary model can solve this problem very well.

Here is an example of the intermediary model in use

Application — Controller switchover

Requirement description: In a root view controller (let’s call it rootVC), click different buttons to jump to the corresponding view controller (firstVC, secondVC, thirdVC).

Traditional approach strategy diagramAs can be seen from the figure, such processing method is complicated and not clear enough. When the number of controller VC is very large, it is more difficult to maintain.

Broker pattern strategy diagramIt can be intuitively felt from the figure that the processing method is concise and clear. Mediator acts as a router, redirecting URL requests from clients to the appropriate class.

Let’s design this class

The design of the Mediator class

The mediator, which should have only one instance throughout the life of the application, will not be able to manage interactions between multiple objects, so the singleton is used here to achieve its creation. I’m going to skip the process and get to the point. First add two controller properties, one representing the controller used to initiate the jump action; The other represents the destination controller. Declare properties and methods in the.h file of the class.

@interface Mediator : NSObject @property (nonatomic, strong, readonly) ViewController *rootVC; @property (nonatomic, strong, readonly) UIViewController *activeVC; // Create a singleton class method + (MediatorManager *)sharedManager; - (void)jumpToNewViewController:(id)tagValue; @endCopy the code

Import all controller header files that need to jump to the. M file

#import "ViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
Copy the code

To achieve the jump method, according to the parameters passed in, decide to jump to which controller VC

- (void)jumpToNewViewController:(id)tagValue{ if ([tagValue isKindOfClass:[NSNumber class]]) { switch ([tagValue integerValue]) { case 1: { FirstViewController *firstVC = [[FirstViewController alloc] init]; _activeVC = firstVC; [self.rootVC.navigationController pushViewController:firstVC animated:YES]; } break; case 2: { SecondViewController *secondVC = [[SecondViewController alloc] init]; _activeVC = secondVC; [self.rootVC.navigationController pushViewController:secondVC animated:YES]; } break; case 3: { ThirdViewController *thirdVC = [[ThirdViewController alloc] init]; _activeVC = thirdVC; [self.rootVC.navigationController pushViewController:thirdVC animated:YES]; } break; Default: // Otherwise, _activeVC = self.rootvc; [self.rootVC.navigationController popViewControllerAnimated:YES]; break; }}}Copy the code

Here, you can optimize a little more, the controller VC corresponding identifier unified in the enumeration variables, convenient management.

typedef NS_ENUM(NSInteger, ButtonTag) {
    TagFirstVC  = 1,
    TagSecondVC = 2,
    TagThirdVC  = 3,
};
Copy the code

conclusion

The application of intermediary mode is not only used in VC management, when the function appears similar to “many to many” complex object group, you can use it to manage these objects. Of course, the first consideration should not be to start using the intermediary pattern, but to consider whether the design of this feature makes sense. In addition, although the use of Mediator mode reduces the coupling between objects and the complexity of logic between objects, the complexity is transferred to the Mediator class to some extent, so the function maintenance of Mediator class needs to be handled carefully.

Other patterns

www.jianshu.com/u/59025f8e5…