mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-29 11:10:19 +02:00
PHP7 Mediator
This commit is contained in:
@@ -4,7 +4,7 @@ namespace DesignPatterns\Behavioral\Mediator;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Colleague is an abstract colleague who works together but he only knows
|
* 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
|
abstract class Colleague
|
||||||
{
|
{
|
||||||
@@ -13,21 +13,13 @@ abstract class Colleague
|
|||||||
*
|
*
|
||||||
* @var MediatorInterface
|
* @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 = $mediator;
|
||||||
$this->mediator = $medium;
|
|
||||||
}
|
|
||||||
|
|
||||||
// for subclasses
|
|
||||||
|
|
||||||
protected function getMediator()
|
|
||||||
{
|
|
||||||
return $this->mediator;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -3,59 +3,54 @@
|
|||||||
namespace DesignPatterns\Behavioral\Mediator;
|
namespace DesignPatterns\Behavioral\Mediator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mediator is the concrete Mediator for this design pattern.
|
* Mediator is the concrete Mediator for this design pattern
|
||||||
* In this example, I have made a "Hello World" with the Mediator Pattern.
|
*
|
||||||
|
* In this example, I have made a "Hello World" with the Mediator Pattern
|
||||||
*/
|
*/
|
||||||
class Mediator implements MediatorInterface
|
class Mediator implements MediatorInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Subsystem\Server
|
* @var Subsystem\Server
|
||||||
*/
|
*/
|
||||||
protected $server;
|
private $server;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Subsystem\Database
|
* @var Subsystem\Database
|
||||||
*/
|
*/
|
||||||
protected $database;
|
private $database;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Subsystem\Client
|
* @var Subsystem\Client
|
||||||
*/
|
*/
|
||||||
protected $client;
|
private $client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Subsystem\Database $db
|
* @param Subsystem\Database $database
|
||||||
* @param Subsystem\Client $cl
|
* @param Subsystem\Client $client
|
||||||
* @param Subsystem\Server $srv
|
* @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->database = $database;
|
||||||
$this->server = $srv;
|
$this->server = $server;
|
||||||
$this->client = $cl;
|
$this->client = $client;
|
||||||
|
|
||||||
|
$this->database->setMediator($this);
|
||||||
|
$this->server->setMediator($this);
|
||||||
|
$this->client->setMediator($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* make request.
|
|
||||||
*/
|
|
||||||
public function makeRequest()
|
public function makeRequest()
|
||||||
{
|
{
|
||||||
$this->server->process();
|
$this->server->process();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function queryDb(): string
|
||||||
* query db.
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function queryDb()
|
|
||||||
{
|
{
|
||||||
return $this->database->getData();
|
return $this->database->getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* send response.
|
|
||||||
*
|
|
||||||
* @param string $content
|
* @param string $content
|
||||||
*/
|
*/
|
||||||
public function sendResponse($content)
|
public function sendResponse($content)
|
||||||
|
@@ -4,7 +4,7 @@ namespace DesignPatterns\Behavioral\Mediator;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* MediatorInterface is a contract for the 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
|
interface MediatorInterface
|
||||||
{
|
{
|
||||||
@@ -16,12 +16,12 @@ interface MediatorInterface
|
|||||||
public function sendResponse($content);
|
public function sendResponse($content);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* makes a request.
|
* makes a request
|
||||||
*/
|
*/
|
||||||
public function makeRequest();
|
public function makeRequest();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* queries the DB.
|
* queries the DB
|
||||||
*/
|
*/
|
||||||
public function queryDb();
|
public function queryDb();
|
||||||
}
|
}
|
||||||
|
@@ -5,24 +5,16 @@ namespace DesignPatterns\Behavioral\Mediator\Subsystem;
|
|||||||
use DesignPatterns\Behavioral\Mediator\Colleague;
|
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
|
class Client extends Colleague
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* request.
|
|
||||||
*/
|
|
||||||
public function request()
|
public function request()
|
||||||
{
|
{
|
||||||
$this->getMediator()->makeRequest();
|
$this->mediator->makeRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function output(string $content)
|
||||||
* output content.
|
|
||||||
*
|
|
||||||
* @param string $content
|
|
||||||
*/
|
|
||||||
public function output($content)
|
|
||||||
{
|
{
|
||||||
echo $content;
|
echo $content;
|
||||||
}
|
}
|
||||||
|
@@ -4,15 +4,9 @@ namespace DesignPatterns\Behavioral\Mediator\Subsystem;
|
|||||||
|
|
||||||
use DesignPatterns\Behavioral\Mediator\Colleague;
|
use DesignPatterns\Behavioral\Mediator\Colleague;
|
||||||
|
|
||||||
/**
|
|
||||||
* Database is a database service.
|
|
||||||
*/
|
|
||||||
class Database extends Colleague
|
class Database extends Colleague
|
||||||
{
|
{
|
||||||
/**
|
public function getData(): string
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getData()
|
|
||||||
{
|
{
|
||||||
return 'World';
|
return 'World';
|
||||||
}
|
}
|
||||||
|
@@ -4,17 +4,11 @@ namespace DesignPatterns\Behavioral\Mediator\Subsystem;
|
|||||||
|
|
||||||
use DesignPatterns\Behavioral\Mediator\Colleague;
|
use DesignPatterns\Behavioral\Mediator\Colleague;
|
||||||
|
|
||||||
/**
|
|
||||||
* Server serves responses.
|
|
||||||
*/
|
|
||||||
class Server extends Colleague
|
class Server extends Colleague
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* process on server.
|
|
||||||
*/
|
|
||||||
public function process()
|
public function process()
|
||||||
{
|
{
|
||||||
$data = $this->getMediator()->queryDb();
|
$data = $this->mediator->queryDb();
|
||||||
$this->getMediator()->sendResponse("Hello $data");
|
$this->mediator->sendResponse(sprintf("Hello %s", $data));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -7,27 +7,14 @@ use DesignPatterns\Behavioral\Mediator\Subsystem\Client;
|
|||||||
use DesignPatterns\Behavioral\Mediator\Subsystem\Database;
|
use DesignPatterns\Behavioral\Mediator\Subsystem\Database;
|
||||||
use DesignPatterns\Behavioral\Mediator\Subsystem\Server;
|
use DesignPatterns\Behavioral\Mediator\Subsystem\Server;
|
||||||
|
|
||||||
/**
|
|
||||||
* MediatorTest tests hello world.
|
|
||||||
*/
|
|
||||||
class MediatorTest extends \PHPUnit_Framework_TestCase
|
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()
|
public function testOutputHelloWorld()
|
||||||
{
|
{
|
||||||
// testing if Hello World is output :
|
$client = new Client();
|
||||||
|
new Mediator(new Database(), $client, new Server());
|
||||||
|
|
||||||
$this->expectOutputString('Hello World');
|
$this->expectOutputString('Hello World');
|
||||||
// as you see, the 3 components Client, Server and Database are totally decoupled
|
$client->request();
|
||||||
$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