From d2e86f1c35074457dc49bad6a7ddca3bc5a9e379 Mon Sep 17 00:00:00 2001 From: Trismegiste Date: Sun, 12 May 2013 14:25:17 +0200 Subject: [PATCH 1/2] mediator in one pass --- Mediator/Colleague.php | 31 +++++++++++++++++++ Mediator/Mediator.php | 53 +++++++++++++++++++++++++++++++++ Mediator/MediatorInterface.php | 21 +++++++++++++ Mediator/Subsystem/Client.php | 27 +++++++++++++++++ Mediator/Subsystem/Database.php | 22 ++++++++++++++ Mediator/Subsystem/Server.php | 23 ++++++++++++++ Tests/Mediator/MediatorTest.php | 36 ++++++++++++++++++++++ 7 files changed, 213 insertions(+) create mode 100644 Mediator/Colleague.php create mode 100644 Mediator/Mediator.php create mode 100644 Mediator/MediatorInterface.php create mode 100644 Mediator/Subsystem/Client.php create mode 100644 Mediator/Subsystem/Database.php create mode 100644 Mediator/Subsystem/Server.php create mode 100644 Tests/Mediator/MediatorTest.php diff --git a/Mediator/Colleague.php b/Mediator/Colleague.php new file mode 100644 index 0000000..e410e86 --- /dev/null +++ b/Mediator/Colleague.php @@ -0,0 +1,31 @@ +mediator; + } + + public function __construct(MediatorInterface $medium) + { + // in this way, we are sure the concrete colleague knows the mediator + $this->mediator = $medium; + } + +} \ No newline at end of file diff --git a/Mediator/Mediator.php b/Mediator/Mediator.php new file mode 100644 index 0000000..d6edb8c --- /dev/null +++ b/Mediator/Mediator.php @@ -0,0 +1,53 @@ +database = $db; + $this->server = $srv; + $this->client = $cl; + } + + public function makeRequest() + { + $this->server->process(); + } + + public function queryDb() + { + return $this->database->getData(); + } + + public function sendResponse($content) + { + $this->client->output($content); + } + +} \ No newline at end of file diff --git a/Mediator/MediatorInterface.php b/Mediator/MediatorInterface.php new file mode 100644 index 0000000..019ad4e --- /dev/null +++ b/Mediator/MediatorInterface.php @@ -0,0 +1,21 @@ +getMediator()->makeRequest(); + } + + public function output($content) + { + echo $content; + } + +} \ No newline at end of file diff --git a/Mediator/Subsystem/Database.php b/Mediator/Subsystem/Database.php new file mode 100644 index 0000000..dd7bb01 --- /dev/null +++ b/Mediator/Subsystem/Database.php @@ -0,0 +1,22 @@ +getMediator()->queryDb(); + $this->getMediator()->sendResponse("Hello $data"); + } + +} \ No newline at end of file diff --git a/Tests/Mediator/MediatorTest.php b/Tests/Mediator/MediatorTest.php new file mode 100644 index 0000000..b673985 --- /dev/null +++ b/Tests/Mediator/MediatorTest.php @@ -0,0 +1,36 @@ +client = new Client($media); + $media->setColleague(new Database($media), $this->client, new Server($media)); + } + + public function testOutputHelloWorld() + { + // testing if Hello World is output : + $this->expectOutputString('Hello World'); + $this->client->request(); + } + +} \ No newline at end of file From a3648c449e29eaa98f1dac5dfc27e7f56b5d3a86 Mon Sep 17 00:00:00 2001 From: Trismegiste Date: Sun, 12 May 2013 14:32:16 +0200 Subject: [PATCH 2/2] adding comments --- Mediator/Mediator.php | 5 +++-- README.markdown | 2 +- Tests/Mediator/MediatorTest.php | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Mediator/Mediator.php b/Mediator/Mediator.php index d6edb8c..98b34a1 100644 --- a/Mediator/Mediator.php +++ b/Mediator/Mediator.php @@ -15,8 +15,9 @@ use DesignPatterns\Mediator\Subsystem; * It is a good alternative over Observer IF you have a "central intelligence", * like a controller (but not in the sense of the MVC). * - * All objects (called Colleague) are coupled only to the MediatorInterface and - * it is a good thing because in OOP, one good friend is better than many unreliable. + * All components (called Colleague) are only coupled to the MediatorInterface and + * it is a good thing because in OOP, one good friend is better than many. This + * is the key-feature of this pattern. * * In this example, I have made a "Hello World" with the Mediator Pattern, have fun (^_^) */ diff --git a/README.markdown b/README.markdown index 3742786..168fc4a 100644 --- a/README.markdown +++ b/README.markdown @@ -23,4 +23,4 @@ Changes I've made : * Fixing the Adapter Pattern because it was buggy and incomplete (+ Tests) * Adding Template Method Pattern (+ Tests) * Adding Builder Pattern (+ Tests) -* \ No newline at end of file +* Adding Mediator Pattern (+ Tests) \ No newline at end of file diff --git a/Tests/Mediator/MediatorTest.php b/Tests/Mediator/MediatorTest.php index b673985..f4f689d 100644 --- a/Tests/Mediator/MediatorTest.php +++ b/Tests/Mediator/MediatorTest.php @@ -30,7 +30,10 @@ class MediatorTest extends \PHPUnit_Framework_TestCase { // testing if Hello World is output : $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. } } \ No newline at end of file