Broker Mode and PHP implementation

Source: Internet
Author: User

Broker Mode:
The Mediator pattern (mediator pattern) Definition: A Mediation object encapsulates a series of object interactions so that the intermediaries do not need to explicitly reference each other, so that they are loosely coupled and can independently change the interaction between them. The mediator pattern, also known as the mediator pattern, is an object-behavioral 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.

The broker pattern contains the following roles:
Mediator: An abstract intermediary that defines the methods that each colleague needs to interact with each other.
Concretemediator: A specific intermediary, it needs to understand and to maintain each colleague object, and is responsible for the specific coordination of the interaction of each colleague object.
Colleague: Abstract colleague classes, often implemented as abstract classes, are primarily responsible for constraining the types of colleague objects and implementing common functions between specific colleague classes
Concretecolleague: The specific colleague class, realizes own business, needs to interact with other colleague objects, notifies the intermediary object, the intermediary object will be responsible for the subsequent interaction

UML Class Diagrams:

  

Code implementation:

Header("content-type:text/html; Charset=utf-8 ");//abstract colleague class, tutorAbstract classtutor{protected $message;//Personal Information    protected $mediator;//an intermediary who serves as a tutor    function__construct ($message, mediator$mediator){        $this->message =$message; $this->mediator =$mediator; }    //Get personal Information    functionGetMessage () {return $this-message; }    //Find Students    Abstract functionfindstudent ();}//specific colleague class, college tutorclassUndergraduatetutorextendstutor{//Tutor Type    Public $type= "Undergraduatetutor"; function__construct ($message, mediator$mediator) {Parent:: __construct ($message,$mediator); }   //find a student and let the agency look for it   functionfindstudent () {$this->mediator->matchstudent ($this); }}//specific colleague class, high school tutorclassSeniorstudenttutorextendstutor{//Tutor Type    Public $type= "Seniorstudenttutor"; function__construct ($message, mediator$mediator) {Parent:: __construct ($message,$mediator); }   //find a student and let the agency look for it   functionfindstudent () {$this->mediator->matchstudent ($this); }}//specific colleague class, Junior high School tutorclassMiddlestudenttutorextendstutor{//Tutor Type    Public $type= "Middlestudenttutor"; function__construct ($message, mediator$mediator) {Parent:: __construct ($message,$mediator); }   //find a student and let the agency look for it   functionfindstudent () {$this->mediator->matchstudent ($this); }}//Abstract Mediation ClassAbstract classabstractmediator{Abstract functionMatchstudent (Tutor$tutor);}//specific intermediary classes for tutors to match the right studentsclassMediatorextendsabstractmediator{//define its services for all tutors, not within range of service    Private $serveObject=Array("Undergraduatetutor", "Seniorstudenttutor", "Middlestudenttutor"); //Matching students    functionMatchstudent (Tutor$tutor){          for($i= 0;$i<Count($this->serveobject);$i++){              if($tutor->type = =$this->serveobject[$i]){                  $message=$tutor-GetMessage (); Echo"The personal information of the tutor is".Print_r($message)." <br/> will match them to the right students ";  Break; }         }         if($i>=Count($this-serveobject)) {             Echo"The tutor is not in the service of my intermediary agency"; }    }}//Test$mediator=NewMediator ();$undergraduateTutor=NewUndergraduatetutor (Array("Name" = "Zhang San", "Age" =>22),$mediator);$undergraduateTutor->findstudent ();
Result: The personal information of the tutor is an Array ([name] = Zhang San [age] + 22), which will match the appropriate student

Broker Mode and PHP implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.