Mediator ---- One of the behavioral design patterns

zhaozj2021-02-12  174

Mediator uses a mediation object to encapsulate a series of object interactions.

Why use Mediator? There are many interactions between individual objects; each object's behavior operation relies on each other to modify the behavior of an object, and it will involve modifying many other objects. If you use Mediator mode, you can make each object. The coupling between the objects is loose, just care about the relationship between the Mediator, so that many-to-many relationships become a pair of relationships, which can reduce the complexity of the system and improve the scalability.

example

Class Mediator; class a {? mediator * _mediator; public:? a (mediator * pmediator): _ mediator (pmediator) {}? ~ a ()? {?? if (_mediator) ??? delete _mediator;?}? Void FA1 ()? {?? _ mediator-> Oper1 ();}? void fa2 ()? {???}}; class b {? Mediator * _mediator; public:? b (Mediator * pmediator): _ mediator (PMediator) ) {}? ~ B ()? {?? if (_mediator) ??? delete _mediator;?}? Void fb1 ()? {???}? Void fb2 ()? {?? _ mediator-> Oper2 () ;?}}; Class Mediator {public:? Mediator ()? {?? _ a._mediator = this; ?? _ b._mediator = this;?}? void Oper1 ()? {?? _ b.fb1 ();? }? void Oper2 ()? {?? _ a.fa2 () ;?}? void test1 ()? {?? _ a.fa1 ()}? void test2 ()? {?? _ b.fb2 (); ? 0}?

// client void main () {? Mediator Med;? Med.test1 ();

Med.test2 ();

转载请注明原文地址:https://www.9cbs.com/read-6321.html

New Post(0)