PHP design pattern series-Mediator pattern)

Source: Internet
Author: User
PHP design pattern series-Mediator pattern 1. Pattern Definition

The Mediator mode uses an intermediary object to encapsulate a series of object interactions. the intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose, in addition, the interaction between them can be changed independently.

For an intermediary object, all objects that interact with each other are considered as the colleague class. the intermediary object is used to maintain the relationship between the objects of each colleague. all the colleague classes only interact with the intermediary object, that is to say, the intermediary object needs to know all the colleagues' objects. When a colleague object changes, it does not know what impact it will have on other colleagues' objects. it only needs to notify the intermediary object, "I changed ", the intermediary object interacts with other colleagues' objects. In this way, the dependencies between the colleague objects are gone. With the intermediary, all interactions are encapsulated in the intermediary object. each object only needs to care about what it can do, you do not need to worry about the impact on other objects, that is, you do not need to maintain these relationships.

2. UML class diagram

3. sample code MediatorInterface. php
 Mediator. php
  Database = $ db; $ this-> server = $ srv; $ this-> client = $ cl;}/*** initiate a request */public function makeRequest () {$ this-> server-> process ();}/*** query database * @ return mixed */public function queryDb () {return $ this-> database-> getData ();}/*** send response ** @ param string $ content */public function sendResponse ($ content) {$ this-> client-> output ($ content );}}
Colleague. php
  Mediator = $ medium;} // for subclasses protected function getMediator () {return $ this-> mediator ;}}
Subsystem/Client. php
  GetMediator ()-> makeRequest ();}/*** output content ** @ param string $ content */public function output ($ content) {echo $ content ;}}
Subsystem/Database. php
  Subsystem/Server. php
   GetMediator ()-> queryDb (); $ this-> getMediator ()-> sendResponse ("Hello $ data ");}}
4. test code Tests/MediatorTest. php
   Client = new Client ($ media); $ media-> setcollegou (new Database ($ media), $ this-> client, new Server ($ media ));} public function testOutputHelloWorld () {// test whether to output Hello World: $ this-> expectOutputString ('Hello World'); // as you can see, Client, server and Database are completely decoupled $ this-> client-> request ();}}
5. Summary

The intermediary is mainly to encapsulate the relationship between objects through the intermediary object, so that each object can communicate with it through the intermediary object without knowing the specific information of other objects. Meanwhile, the relationship between system objects is reduced by referencing intermediary objects, which improves object reusability and system scalability. However, because the intermediary object encapsulates the associations between objects, the intermediary object becomes large and has more responsibilities. It needs to know the interaction details between each object and them. if it goes wrong, it will cause problems in the entire system.

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.