diff --git a/Behavioral/Mediator/Colleague.php b/Behavioral/Mediator/Colleague.php index c74dee5..3a83742 100644 --- a/Behavioral/Mediator/Colleague.php +++ b/Behavioral/Mediator/Colleague.php @@ -4,7 +4,7 @@ namespace DesignPatterns\Behavioral\Mediator; /** * Colleague is an abstract colleague who works together but he only knows - * the Mediator, not other colleague. + * the Mediator, not other colleagues */ abstract class Colleague { @@ -13,21 +13,13 @@ abstract class Colleague * * @var MediatorInterface */ - private $mediator; + protected $mediator; /** - * @param MediatorInterface $medium + * @param MediatorInterface $mediator */ - public function __construct(MediatorInterface $medium) + public function setMediator(MediatorInterface $mediator) { - // in this way, we are sure the concrete colleague knows the mediator - $this->mediator = $medium; - } - - // for subclasses - - protected function getMediator() - { - return $this->mediator; + $this->mediator = $mediator; } } diff --git a/Behavioral/Mediator/Mediator.php b/Behavioral/Mediator/Mediator.php index 98a7890..08e1b71 100644 --- a/Behavioral/Mediator/Mediator.php +++ b/Behavioral/Mediator/Mediator.php @@ -3,59 +3,54 @@ namespace DesignPatterns\Behavioral\Mediator; /** - * Mediator is the concrete Mediator for this design pattern. - * In this example, I have made a "Hello World" with the Mediator Pattern. + * Mediator is the concrete Mediator for this design pattern + * + * In this example, I have made a "Hello World" with the Mediator Pattern */ class Mediator implements MediatorInterface { /** * @var Subsystem\Server */ - protected $server; + private $server; /** * @var Subsystem\Database */ - protected $database; + private $database; /** * @var Subsystem\Client */ - protected $client; + private $client; /** - * @param Subsystem\Database $db - * @param Subsystem\Client $cl - * @param Subsystem\Server $srv + * @param Subsystem\Database $database + * @param Subsystem\Client $client + * @param Subsystem\Server $server */ - public function setColleague(Subsystem\Database $db, Subsystem\Client $cl, Subsystem\Server $srv) + public function __construct(Subsystem\Database $database, Subsystem\Client $client, Subsystem\Server $server) { - $this->database = $db; - $this->server = $srv; - $this->client = $cl; + $this->database = $database; + $this->server = $server; + $this->client = $client; + + $this->database->setMediator($this); + $this->server->setMediator($this); + $this->client->setMediator($this); } - /** - * make request. - */ public function makeRequest() { $this->server->process(); } - /** - * query db. - * - * @return mixed - */ - public function queryDb() + public function queryDb(): string { return $this->database->getData(); } /** - * send response. - * * @param string $content */ public function sendResponse($content) diff --git a/Behavioral/Mediator/MediatorInterface.php b/Behavioral/Mediator/MediatorInterface.php index dbdd489..5ec717a 100644 --- a/Behavioral/Mediator/MediatorInterface.php +++ b/Behavioral/Mediator/MediatorInterface.php @@ -4,7 +4,7 @@ namespace DesignPatterns\Behavioral\Mediator; /** * MediatorInterface is a contract for the Mediator - * This interface is not mandatory but it is better for LSP concerns. + * This interface is not mandatory but it is better for Liskov substitution principle concerns. */ interface MediatorInterface { @@ -16,12 +16,12 @@ interface MediatorInterface public function sendResponse($content); /** - * makes a request. + * makes a request */ public function makeRequest(); /** - * queries the DB. + * queries the DB */ public function queryDb(); } diff --git a/Behavioral/Mediator/Subsystem/Client.php b/Behavioral/Mediator/Subsystem/Client.php index f7a21c9..e7897a4 100644 --- a/Behavioral/Mediator/Subsystem/Client.php +++ b/Behavioral/Mediator/Subsystem/Client.php @@ -5,24 +5,16 @@ namespace DesignPatterns\Behavioral\Mediator\Subsystem; use DesignPatterns\Behavioral\Mediator\Colleague; /** - * Client is a client that make request et get response. + * Client is a client that makes requests and gets the response response. */ class Client extends Colleague { - /** - * request. - */ public function request() { - $this->getMediator()->makeRequest(); + $this->mediator->makeRequest(); } - /** - * output content. - * - * @param string $content - */ - public function output($content) + public function output(string $content) { echo $content; } diff --git a/Behavioral/Mediator/Subsystem/Database.php b/Behavioral/Mediator/Subsystem/Database.php index 69ad6cf..9255f22 100644 --- a/Behavioral/Mediator/Subsystem/Database.php +++ b/Behavioral/Mediator/Subsystem/Database.php @@ -4,15 +4,9 @@ namespace DesignPatterns\Behavioral\Mediator\Subsystem; use DesignPatterns\Behavioral\Mediator\Colleague; -/** - * Database is a database service. - */ class Database extends Colleague { - /** - * @return string - */ - public function getData() + public function getData(): string { return 'World'; } diff --git a/Behavioral/Mediator/Subsystem/Server.php b/Behavioral/Mediator/Subsystem/Server.php index 1602bcb..c05be5e 100644 --- a/Behavioral/Mediator/Subsystem/Server.php +++ b/Behavioral/Mediator/Subsystem/Server.php @@ -4,17 +4,11 @@ namespace DesignPatterns\Behavioral\Mediator\Subsystem; use DesignPatterns\Behavioral\Mediator\Colleague; -/** - * Server serves responses. - */ class Server extends Colleague { - /** - * process on server. - */ public function process() { - $data = $this->getMediator()->queryDb(); - $this->getMediator()->sendResponse("Hello $data"); + $data = $this->mediator->queryDb(); + $this->mediator->sendResponse(sprintf("Hello %s", $data)); } } diff --git a/Behavioral/Mediator/Tests/MediatorTest.php b/Behavioral/Mediator/Tests/MediatorTest.php index 2bce947..2cd40a6 100644 --- a/Behavioral/Mediator/Tests/MediatorTest.php +++ b/Behavioral/Mediator/Tests/MediatorTest.php @@ -7,27 +7,14 @@ use DesignPatterns\Behavioral\Mediator\Subsystem\Client; use DesignPatterns\Behavioral\Mediator\Subsystem\Database; use DesignPatterns\Behavioral\Mediator\Subsystem\Server; -/** - * MediatorTest tests hello world. - */ class MediatorTest extends \PHPUnit_Framework_TestCase { - protected $client; - - protected function setUp() - { - $media = new Mediator(); - $this->client = new Client($media); - $media->setColleague(new Database($media), $this->client, new Server($media)); - } - public function testOutputHelloWorld() { - // testing if Hello World is output : + $client = new Client(); + new Mediator(new Database(), $client, new Server()); + $this->expectOutputString('Hello World'); - // as you see, the 3 components Client, Server and Database are totally decoupled - $this->client->request(); - // Anyway, it remains complexity in the Mediator that's why the pattern - // Observer is preferable in mnay situations. + $client->request(); } }