mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 19:50:12 +02:00
Merge branch 'mediator-pattern'
This commit is contained in:
31
Mediator/Colleague.php
Normal file
31
Mediator/Colleague.php
Normal 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
54
Mediator/Mediator.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
21
Mediator/MediatorInterface.php
Normal file
21
Mediator/MediatorInterface.php
Normal 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();
|
||||
}
|
27
Mediator/Subsystem/Client.php
Normal file
27
Mediator/Subsystem/Client.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
22
Mediator/Subsystem/Database.php
Normal file
22
Mediator/Subsystem/Database.php
Normal 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";
|
||||
}
|
||||
|
||||
}
|
23
Mediator/Subsystem/Server.php
Normal file
23
Mediator/Subsystem/Server.php
Normal 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");
|
||||
}
|
||||
|
||||
}
|
@@ -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)
|
39
Tests/Mediator/MediatorTest.php
Normal file
39
Tests/Mediator/MediatorTest.php
Normal 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.
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user