mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-01 20:50:15 +02:00
mediator in one pass
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
53
Mediator/Mediator.php
Normal file
53
Mediator/Mediator.php
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
36
Tests/Mediator/MediatorTest.php
Normal file
36
Tests/Mediator/MediatorTest.php
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user