Merge branch 'mediator-pattern'

This commit is contained in:
Trismegiste
2013-05-12 14:32:33 +02:00
8 changed files with 218 additions and 1 deletions

31
Mediator/Colleague.php Normal file
View File

@@ -0,0 +1,31 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\Mediator;
/**
* Colleague is an abstract colleague who works together but he only knows
* the Mediator, not other colleague.
*/
abstract class Colleague
{
// this ensures no change in subclasses
private $mediator;
// for subclasses
protected function getMediator()
{
return $this->mediator;
}
public function __construct(MediatorInterface $medium)
{
// in this way, we are sure the concrete colleague knows the mediator
$this->mediator = $medium;
}
}

54
Mediator/Mediator.php Normal file
View File

@@ -0,0 +1,54 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\Mediator;
use DesignPatterns\Mediator\Subsystem;
/**
* Mediator is the concrete Mediator for this design pattern.
*
* This pattern provides an easy to decouple many components working together.
* 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 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 (^_^)
*/
class Mediator implements MediatorInterface
{
// you could have an array
protected $server;
protected $database;
protected $client;
public function setColleague(Subsystem\Database $db, Subsystem\Client $cl, Subsystem\Server $srv)
{
$this->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);
}
}

View File

@@ -0,0 +1,21 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\Mediator;
/**
* MediatorInterface is a contract for the Mediator
* This interface is not mandatory but it is better for LSP concerns
*/
interface MediatorInterface
{
function sendResponse($content);
function makeRequest();
function queryDb();
}

View File

@@ -0,0 +1,27 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\Mediator\Subsystem;
use DesignPatterns\Mediator\Colleague;
/**
* Client is a client that make request et get response
*/
class Client extends Colleague
{
public function request()
{
$this->getMediator()->makeRequest();
}
public function output($content)
{
echo $content;
}
}

View File

@@ -0,0 +1,22 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\Mediator\Subsystem;
use DesignPatterns\Mediator\Colleague;
/**
* Database is a database service
*/
class Database extends Colleague
{
public function getData()
{
return "World";
}
}

View File

@@ -0,0 +1,23 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\Mediator\Subsystem;
use DesignPatterns\Mediator\Colleague;
/**
* Server serves responses
*/
class Server extends Colleague
{
public function process()
{
$data = $this->getMediator()->queryDb();
$this->getMediator()->sendResponse("Hello $data");
}
}

View File

@@ -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)
*
* Adding Mediator Pattern (+ Tests)

View File

@@ -0,0 +1,39 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\Tests\Mediator;
use DesignPatterns\Mediator\Mediator;
use DesignPatterns\Mediator\Subsystem\Database;
use DesignPatterns\Mediator\Subsystem\Client;
use DesignPatterns\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 :
$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.
}
}