1. Pattern definition
The mediator pattern (mediator) is to encapsulate 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.
For a Mediation object, all objects that interact with each other are treated as colleague classes, which are used to maintain relationships between peer objects, and all colleague classes only interact with the mediation object, that is, the mediation object needs to know all of the colleague objects. When a colleague object itself changes, it does not know what effect it will have on other colleague objects, it only needs to notify the mediation object, "I have changed", the mediation object will go to interact with other colleague objects. This way, the dependencies between colleague objects are gone. With intermediaries, all of the interactions are encapsulated within the mediation object, and each object needs to be concerned with what it can do, without having to worry about what it will do to other objects, that is, no longer having to maintain these relationships.
2. UML Class Diagram
3. Sample Code
mediatorinterface.php
mediator.php
Database = $db; $this->server = $srv; $this->client = $CL; }/** * Initiate request */Public Function makerequest () {$this->server->process (); /** * Query Database * @return Mixed */Public Function Querydb () {return $this->database->get Data (); /** * 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->setcolleague (New Database ($media), $this->client, New Server ($media)); } Public Function Testoutputhelloworld () { //test output Hello World: $this->expectoutputstring (' Hello World '); As you can see, Client, Server and Database are fully decoupled $this->client->request ();} }
5. Summary
Intermediaries mainly use intermediary objects to encapsulate the relationships between objects so that each object communicates with the intermediary object without needing to know the specific information of other objects. At the same time, by referring to the intermediary object to reduce the relationship between the system objects, improve the reusability of objects and system scalability. But it is because the intermediary object encapsulates the relationship between objects, resulting in the intermediary objects become larger, the responsibility is more. It needs to know the details of each object and the interaction between them, and if it goes wrong, it will cause the whole system to go wrong.