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..98b34a1 --- /dev/null +++ b/Mediator/Mediator.php @@ -0,0 +1,54 @@ +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/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 new file mode 100644 index 0000000..f4f689d --- /dev/null +++ b/Tests/Mediator/MediatorTest.php @@ -0,0 +1,39 @@ +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'); + // 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