What is the broker mode?
The mediator pattern is used to develop an object that can transfer or mediate the modification of a collection of these objects in situations where similar objects are not directly connected to each other. When dealing with non-coupled objects that have similar properties and need to remain synchronized, the best practice is to broker mode. PHP is not a particularly common design pattern.
Mode motive:
1. In the design of direct chat between user and user, there is a strong correlation between user objects, which will cause the system to have the following problems:
The system structure is complex: there are a lot of correlations and calls between objects, and if one object changes, then all other objects associated with that object need to be tracked and handled appropriately.
2. Poor reusability of objects: Because an object is strongly associated with other objects, it is difficult for an object to be reused by another system or module without the support of other objects, which are more like an indivisible whole and have a confusing responsibility.
3. Low system scalability: Adding a new object needs to add a reference to the original related object, adding a new reference relationship also needs to adjust the original object, the system coupling is very high, the object operation is not flexible, poor extensibility.
4. In the object-oriented software design and development process, according to the "single responsibility Principle", we should try to refine the object, so that it is only responsible for or present a single responsibility.
5. For a module, which may consist of many objects, and there may be mutual references between these objects, in order to reduce the complex reference relationship between object 22, so that it becomes a loosely coupled system, we need to use the mediator pattern, which is the mode of the mediator mode motivation.
Uml
This UML diagram illustrates a class design that uses the mediator design pattern
Here is a description of the pair:
1. There are two similar classes in the diagram: Myobjecta and MYOBJECTB. These two classes appear to be the same shape. Their differences may be identifiers represented by the private variable identifier. In addition, all functions are similar.
2. During object creation, instances of Myobjectmediator are stored internally. Then, if a change to an object is requested by calling the public method Cahngeidentifier (), then the parameter newid can be applied to the object by updating the private identifier string. Next, call the protection Method Notifymediator () to apply the mediation to the rest of the objects.
3.MyObjectMediator is the center of a series of objects. These objects are stored in the array myobjectstomediate. Myobjectstomediate executes the myobjectchanged () method when notified, which parses the array myobjectstomediate and applies the specified changes to all other objects.
Encapsulates a series of object interactions with a mediation object so that the objects do not need to explicitly reference each other so that they are loosely coupled and can independently change the interaction between them
<?php/*** Mediator Mode * * uses a Mediation object to encapsulate a series of object interactions so that the objects do not need to explicitly reference each other so that they are loosely coupled and can independently change the interaction between them */abstract class Mediator{abstract Public function Send ($message, $colleague);} Abstract class Colleague{private $_mediator = Null;public function Colleague ($mediator) {$this->_mediator = $mediator ;} Public function Send ($message) {$this->_mediator->send ($message, $this);} Abstract Public Function notify ($message);} Class Concretemediator extends Mediator{private $_colleague1 = null;private $_colleague2 = null;public function Send ($mes Sage, $colleague) {if ($colleague = = $this->_colleague1) {$this->_colleague1->notify ($message);} else {$this- >_colleague2->notify ($message);}} Public function set ($colleague 1, $colleague 2) {$this->_colleague1 = $colleague 1; $this->_colleague2 = $colleague 2 ;}} Class Colleague1 extends Colleague{public function notify ($message) {echo "Colleague1 message is:". $message. " <br/> ";}} Class Colleague2 extends Colleague{public function notify ($message) {echo ' Colleague2Message is: ". $message." <br/> ";}} $objMediator = new Concretemediator (), $objC 1 = new Colleague1 ($objMediator), $objC 2 = new Colleague2 ($objMediator); Objmediator->set ($objC 1, $objC 2), $objC 1->send ("to C2 from C1"), $objC 2->send ("to C1 from C2");