3.4. Mediator

3.4.1. Purpose

This pattern provides an easy way to decouple many components workingtogether. It is a good alternative to Observer IF you have a “centralintelligence”, like a controller (but not in the sense of the MVC).

All components (called Colleague) are only coupled to theMediatorInterface and it is a good thing because in OOP, one good friendis better than many. This is the key-feature of this pattern.

3.4.2. UML Diagram

Alt Mediator UML Diagram

3.4.3. Code

You can also find this code on GitHub

MediatorInterface.php

  1. <?php
  2.  
  3. namespace DesignPatterns\Behavioral\Mediator;
  4.  
  5. /**
  6. * MediatorInterface is a contract for the Mediator
  7. * This interface is not mandatory but it is better for Liskov substitution principle concerns.
  8. */
  9. interface MediatorInterface
  10. {
  11. /**
  12. * sends the response.
  13. *
  14. * @param string $content
  15. */
  16. public function sendResponse($content);
  17.  
  18. /**
  19. * makes a request
  20. */
  21. public function makeRequest();
  22.  
  23. /**
  24. * queries the DB
  25. */
  26. public function queryDb();
  27. }

Mediator.php

  1. <?php
  2.  
  3. namespace DesignPatterns\Behavioral\Mediator;
  4.  
  5. /**
  6. * Mediator is the concrete Mediator for this design pattern
  7. *
  8. * In this example, I have made a "Hello World" with the Mediator Pattern
  9. */
  10. class Mediator implements MediatorInterface
  11. {
  12. /**
  13. * @var Subsystem\Server
  14. */
  15. private $server;
  16.  
  17. /**
  18. * @var Subsystem\Database
  19. */
  20. private $database;
  21.  
  22. /**
  23. * @var Subsystem\Client
  24. */
  25. private $client;
  26.  
  27. /**
  28. * @param Subsystem\Database $database
  29. * @param Subsystem\Client $client
  30. * @param Subsystem\Server $server
  31. */
  32. public function __construct(Subsystem\Database $database, Subsystem\Client $client, Subsystem\Server $server)
  33. {
  34. $this->database = $database;
  35. $this->server = $server;
  36. $this->client = $client;
  37.  
  38. $this->database->setMediator($this);
  39. $this->server->setMediator($this);
  40. $this->client->setMediator($this);
  41. }
  42.  
  43. public function makeRequest()
  44. {
  45. $this->server->process();
  46. }
  47.  
  48. public function queryDb(): string
  49. {
  50. return $this->database->getData();
  51. }
  52.  
  53. /**
  54. * @param string $content
  55. */
  56. public function sendResponse($content)
  57. {
  58. $this->client->output($content);
  59. }
  60. }

Colleague.php

  1. <?php
  2.  
  3. namespace DesignPatterns\Behavioral\Mediator;
  4.  
  5. /**
  6. * Colleague is an abstract colleague who works together but he only knows
  7. * the Mediator, not other colleagues
  8. */
  9. abstract class Colleague
  10. {
  11. /**
  12. * this ensures no change in subclasses.
  13. *
  14. * @var MediatorInterface
  15. */
  16. protected $mediator;
  17.  
  18. /**
  19. * @param MediatorInterface $mediator
  20. */
  21. public function setMediator(MediatorInterface $mediator)
  22. {
  23. $this->mediator = $mediator;
  24. }
  25. }

Subsystem/Client.php

  1. <?php
  2.  
  3. namespace DesignPatterns\Behavioral\Mediator\Subsystem;
  4.  
  5. use DesignPatterns\Behavioral\Mediator\Colleague;
  6.  
  7. /**
  8. * Client is a client that makes requests and gets the response.
  9. */
  10. class Client extends Colleague
  11. {
  12. public function request()
  13. {
  14. $this->mediator->makeRequest();
  15. }
  16.  
  17. public function output(string $content)
  18. {
  19. echo $content;
  20. }
  21. }

Subsystem/Database.php

  1. <?php
  2.  
  3. namespace DesignPatterns\Behavioral\Mediator\Subsystem;
  4.  
  5. use DesignPatterns\Behavioral\Mediator\Colleague;
  6.  
  7. class Database extends Colleague
  8. {
  9. public function getData(): string
  10. {
  11. return 'World';
  12. }
  13. }

Subsystem/Server.php

  1. <?php
  2.  
  3. namespace DesignPatterns\Behavioral\Mediator\Subsystem;
  4.  
  5. use DesignPatterns\Behavioral\Mediator\Colleague;
  6.  
  7. class Server extends Colleague
  8. {
  9. public function process()
  10. {
  11. $data = $this->mediator->queryDb();
  12. $this->mediator->sendResponse(sprintf("Hello %s", $data));
  13. }
  14. }

3.4.4. Test

Tests/MediatorTest.php

  1. <?php
  2.  
  3. namespace DesignPatterns\Tests\Mediator\Tests;
  4.  
  5. use DesignPatterns\Behavioral\Mediator\Mediator;
  6. use DesignPatterns\Behavioral\Mediator\Subsystem\Client;
  7. use DesignPatterns\Behavioral\Mediator\Subsystem\Database;
  8. use DesignPatterns\Behavioral\Mediator\Subsystem\Server;
  9. use PHPUnit\Framework\TestCase;
  10.  
  11. class MediatorTest extends TestCase
  12. {
  13. public function testOutputHelloWorld()
  14. {
  15. $client = new Client();
  16. new Mediator(new Database(), $client, new Server());
  17.  
  18. $this->expectOutputString('Hello World');
  19. $client->request();
  20. }
  21. }