PHP7 Mediator

This commit is contained in:
Dominik Liebler
2016-09-22 09:38:55 +02:00
parent a6b09d6b18
commit 64e21e8581
7 changed files with 36 additions and 82 deletions

View File

@@ -4,7 +4,7 @@ namespace DesignPatterns\Behavioral\Mediator;
/**
* Colleague is an abstract colleague who works together but he only knows
* the Mediator, not other colleague.
* the Mediator, not other colleagues
*/
abstract class Colleague
{
@@ -13,21 +13,13 @@ abstract class Colleague
*
* @var MediatorInterface
*/
private $mediator;
protected $mediator;
/**
* @param MediatorInterface $medium
* @param MediatorInterface $mediator
*/
public function __construct(MediatorInterface $medium)
public function setMediator(MediatorInterface $mediator)
{
// in this way, we are sure the concrete colleague knows the mediator
$this->mediator = $medium;
}
// for subclasses
protected function getMediator()
{
return $this->mediator;
$this->mediator = $mediator;
}
}

View File

@@ -3,59 +3,54 @@
namespace DesignPatterns\Behavioral\Mediator;
/**
* Mediator is the concrete Mediator for this design pattern.
* In this example, I have made a "Hello World" with the Mediator Pattern.
* 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
{
/**
* @var Subsystem\Server
*/
protected $server;
private $server;
/**
* @var Subsystem\Database
*/
protected $database;
private $database;
/**
* @var Subsystem\Client
*/
protected $client;
private $client;
/**
* @param Subsystem\Database $db
* @param Subsystem\Client $cl
* @param Subsystem\Server $srv
* @param Subsystem\Database $database
* @param Subsystem\Client $client
* @param Subsystem\Server $server
*/
public function setColleague(Subsystem\Database $db, Subsystem\Client $cl, Subsystem\Server $srv)
public function __construct(Subsystem\Database $database, Subsystem\Client $client, Subsystem\Server $server)
{
$this->database = $db;
$this->server = $srv;
$this->client = $cl;
$this->database = $database;
$this->server = $server;
$this->client = $client;
$this->database->setMediator($this);
$this->server->setMediator($this);
$this->client->setMediator($this);
}
/**
* make request.
*/
public function makeRequest()
{
$this->server->process();
}
/**
* query db.
*
* @return mixed
*/
public function queryDb()
public function queryDb(): string
{
return $this->database->getData();
}
/**
* send response.
*
* @param string $content
*/
public function sendResponse($content)

View File

@@ -4,7 +4,7 @@ namespace DesignPatterns\Behavioral\Mediator;
/**
* MediatorInterface is a contract for the Mediator
* This interface is not mandatory but it is better for LSP concerns.
* This interface is not mandatory but it is better for Liskov substitution principle concerns.
*/
interface MediatorInterface
{
@@ -16,12 +16,12 @@ interface MediatorInterface
public function sendResponse($content);
/**
* makes a request.
* makes a request
*/
public function makeRequest();
/**
* queries the DB.
* queries the DB
*/
public function queryDb();
}

View File

@@ -5,24 +5,16 @@ namespace DesignPatterns\Behavioral\Mediator\Subsystem;
use DesignPatterns\Behavioral\Mediator\Colleague;
/**
* Client is a client that make request et get response.
* Client is a client that makes requests and gets the response response.
*/
class Client extends Colleague
{
/**
* request.
*/
public function request()
{
$this->getMediator()->makeRequest();
$this->mediator->makeRequest();
}
/**
* output content.
*
* @param string $content
*/
public function output($content)
public function output(string $content)
{
echo $content;
}

View File

@@ -4,15 +4,9 @@ namespace DesignPatterns\Behavioral\Mediator\Subsystem;
use DesignPatterns\Behavioral\Mediator\Colleague;
/**
* Database is a database service.
*/
class Database extends Colleague
{
/**
* @return string
*/
public function getData()
public function getData(): string
{
return 'World';
}

View File

@@ -4,17 +4,11 @@ namespace DesignPatterns\Behavioral\Mediator\Subsystem;
use DesignPatterns\Behavioral\Mediator\Colleague;
/**
* Server serves responses.
*/
class Server extends Colleague
{
/**
* process on server.
*/
public function process()
{
$data = $this->getMediator()->queryDb();
$this->getMediator()->sendResponse("Hello $data");
$data = $this->mediator->queryDb();
$this->mediator->sendResponse(sprintf("Hello %s", $data));
}
}

View File

@@ -7,27 +7,14 @@ use DesignPatterns\Behavioral\Mediator\Subsystem\Client;
use DesignPatterns\Behavioral\Mediator\Subsystem\Database;
use DesignPatterns\Behavioral\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 :
$client = new Client();
new Mediator(new Database(), $client, new Server());
$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.
$client->request();
}
}