mediator in one pass

This commit is contained in:
Trismegiste
2013-05-12 14:25:17 +02:00
parent f52ebfd74f
commit d2e86f1c35
7 changed files with 213 additions and 0 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;
}
}

53
Mediator/Mediator.php Normal file
View File

@@ -0,0 +1,53 @@
<?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 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.
*
* 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

@@ -0,0 +1,36 @@
<?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');
$this->client->request();
}
}