start a restructure

This commit is contained in:
Antonio Spinelli
2014-03-21 18:03:44 -03:00
parent b0b0d4a1a4
commit e59d70a0ac
180 changed files with 21 additions and 16 deletions

View File

@@ -0,0 +1,32 @@
<?php
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
*
* @var MediatorInterface
*/
private $mediator;
// for subclasses
protected function getMediator()
{
return $this->mediator;
}
/**
* @param MediatorInterface $medium
*/
public function __construct(MediatorInterface $medium)
{
// in this way, we are sure the concrete colleague knows the mediator
$this->mediator = $medium;
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace DesignPatterns\Mediator;
use DesignPatterns\Mediator\Subsystem;
/**
* 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
{
// you could have an array
protected $server;
protected $database;
protected $client;
/**
* @param Subsystem\Database $db
* @param Subsystem\Client $cl
* @param Subsystem\Server $srv
*/
public function setColleague(Subsystem\Database $db, Subsystem\Client $cl, Subsystem\Server $srv)
{
$this->database = $db;
$this->server = $srv;
$this->client = $cl;
}
/**
* make request
*/
public function makeRequest()
{
$this->server->process();
}
/**
* query db
*
* @return mixed
*/
public function queryDb()
{
return $this->database->getData();
}
/**
* send response
*
* @param string $content
*/
public function sendResponse($content)
{
$this->client->output($content);
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace DesignPatterns\Mediator;
/**
* MediatorInterface is a contract for the Mediator
* This interface is not mandatory but it is better for LSP concerns
*/
interface MediatorInterface
{
/**
* sends the response
*
* @param string $content
*/
public function sendResponse($content);
/**
* makes a request
*/
public function makeRequest();
/**
* queries the DB
*/
public function queryDb();
}

View File

@@ -0,0 +1,11 @@
# Mediator
## Purpose
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.

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,34 @@
<?php
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.
}
}