mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-29 11:10:19 +02:00
reworked the Mediator pattern
This commit is contained in:
@@ -1,47 +0,0 @@
|
|||||||
<?php declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace DesignPatterns\Behavioral\Mediator;
|
|
||||||
|
|
||||||
class ConcreteMediator implements Mediator
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var Subsystem\Server
|
|
||||||
*/
|
|
||||||
private $server;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Subsystem\Database
|
|
||||||
*/
|
|
||||||
private $database;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Subsystem\Client
|
|
||||||
*/
|
|
||||||
private $client;
|
|
||||||
|
|
||||||
public function __construct(Subsystem\Database $database, Subsystem\Client $client, Subsystem\Server $server)
|
|
||||||
{
|
|
||||||
$this->database = $database;
|
|
||||||
$this->server = $server;
|
|
||||||
$this->client = $client;
|
|
||||||
|
|
||||||
$this->database->setMediator($this);
|
|
||||||
$this->server->setMediator($this);
|
|
||||||
$this->client->setMediator($this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function makeRequest()
|
|
||||||
{
|
|
||||||
$this->server->process();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function queryDb(): string
|
|
||||||
{
|
|
||||||
return $this->database->getData();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function sendResponse(string $content)
|
|
||||||
{
|
|
||||||
$this->client->output($content);
|
|
||||||
}
|
|
||||||
}
|
|
@@ -4,9 +4,5 @@ namespace DesignPatterns\Behavioral\Mediator;
|
|||||||
|
|
||||||
interface Mediator
|
interface Mediator
|
||||||
{
|
{
|
||||||
public function sendResponse(string $content);
|
public function getUser(string $username): string;
|
||||||
|
|
||||||
public function makeRequest();
|
|
||||||
|
|
||||||
public function queryDb();
|
|
||||||
}
|
}
|
||||||
|
@@ -30,33 +30,27 @@ Mediator.php
|
|||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
ConcreteMediator.php
|
|
||||||
|
|
||||||
.. literalinclude:: ConcreteMediator.php
|
|
||||||
:language: php
|
|
||||||
:linenos:
|
|
||||||
|
|
||||||
Colleague.php
|
Colleague.php
|
||||||
|
|
||||||
.. literalinclude:: Colleague.php
|
.. literalinclude:: Colleague.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
Subsystem/Client.php
|
Ui.php
|
||||||
|
|
||||||
.. literalinclude:: Subsystem/Client.php
|
.. literalinclude:: Ui.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
Subsystem/Database.php
|
UserRepository.php
|
||||||
|
|
||||||
.. literalinclude:: Subsystem/Database.php
|
.. literalinclude:: UserRepository.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
Subsystem/Server.php
|
UserRepositoryUiMediator.php
|
||||||
|
|
||||||
.. literalinclude:: Subsystem/Server.php
|
.. literalinclude:: UserRepositoryUiMediator.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
|
@@ -1,21 +0,0 @@
|
|||||||
<?php declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace DesignPatterns\Behavioral\Mediator\Subsystem;
|
|
||||||
|
|
||||||
use DesignPatterns\Behavioral\Mediator\Colleague;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Client is a client that makes requests and gets the response.
|
|
||||||
*/
|
|
||||||
class Client extends Colleague
|
|
||||||
{
|
|
||||||
public function request()
|
|
||||||
{
|
|
||||||
$this->mediator->makeRequest();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function output(string $content)
|
|
||||||
{
|
|
||||||
echo $content;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,13 +0,0 @@
|
|||||||
<?php declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace DesignPatterns\Behavioral\Mediator\Subsystem;
|
|
||||||
|
|
||||||
use DesignPatterns\Behavioral\Mediator\Colleague;
|
|
||||||
|
|
||||||
class Database extends Colleague
|
|
||||||
{
|
|
||||||
public function getData(): string
|
|
||||||
{
|
|
||||||
return 'World';
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,14 +0,0 @@
|
|||||||
<?php declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace DesignPatterns\Behavioral\Mediator\Subsystem;
|
|
||||||
|
|
||||||
use DesignPatterns\Behavioral\Mediator\Colleague;
|
|
||||||
|
|
||||||
class Server extends Colleague
|
|
||||||
{
|
|
||||||
public function process()
|
|
||||||
{
|
|
||||||
$data = $this->mediator->queryDb();
|
|
||||||
$this->mediator->sendResponse(sprintf("Hello %s", $data));
|
|
||||||
}
|
|
||||||
}
|
|
@@ -2,20 +2,18 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Tests\Mediator\Tests;
|
namespace DesignPatterns\Tests\Mediator\Tests;
|
||||||
|
|
||||||
use DesignPatterns\Behavioral\Mediator\ConcreteMediator;
|
use DesignPatterns\Behavioral\Mediator\Ui;
|
||||||
use DesignPatterns\Behavioral\Mediator\Subsystem\Client;
|
use DesignPatterns\Behavioral\Mediator\UserRepository;
|
||||||
use DesignPatterns\Behavioral\Mediator\Subsystem\Database;
|
use DesignPatterns\Behavioral\Mediator\UserRepositoryUiMediator;
|
||||||
use DesignPatterns\Behavioral\Mediator\Subsystem\Server;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class MediatorTest extends TestCase
|
class MediatorTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testOutputHelloWorld()
|
public function testOutputHelloWorld()
|
||||||
{
|
{
|
||||||
$client = new Client();
|
$mediator = new UserRepositoryUiMediator(new UserRepository(), new UI());
|
||||||
new ConcreteMediator(new Database(), $client, new Server());
|
|
||||||
|
|
||||||
$this->expectOutputString('Hello World');
|
$this->expectOutputString('User: Dominik');
|
||||||
$client->request();
|
$mediator->printInfoAbout('Dominik');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
11
Behavioral/Mediator/Ui.php
Normal file
11
Behavioral/Mediator/Ui.php
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DesignPatterns\Behavioral\Mediator;
|
||||||
|
|
||||||
|
class Ui extends Colleague
|
||||||
|
{
|
||||||
|
public function outputUserInfo(string $username)
|
||||||
|
{
|
||||||
|
echo $this->mediator->getUser($username);
|
||||||
|
}
|
||||||
|
}
|
11
Behavioral/Mediator/UserRepository.php
Normal file
11
Behavioral/Mediator/UserRepository.php
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DesignPatterns\Behavioral\Mediator;
|
||||||
|
|
||||||
|
class UserRepository extends Colleague
|
||||||
|
{
|
||||||
|
public function getUserName(string $user): string
|
||||||
|
{
|
||||||
|
return 'User: ' . $user;
|
||||||
|
}
|
||||||
|
}
|
35
Behavioral/Mediator/UserRepositoryUiMediator.php
Normal file
35
Behavioral/Mediator/UserRepositoryUiMediator.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DesignPatterns\Behavioral\Mediator;
|
||||||
|
|
||||||
|
class UserRepositoryUiMediator implements Mediator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var UserRepository
|
||||||
|
*/
|
||||||
|
private $userRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Ui
|
||||||
|
*/
|
||||||
|
private $ui;
|
||||||
|
|
||||||
|
public function __construct(UserRepository $userRepository, Ui $ui)
|
||||||
|
{
|
||||||
|
$this->userRepository = $userRepository;
|
||||||
|
$this->ui = $ui;
|
||||||
|
|
||||||
|
$this->userRepository->setMediator($this);
|
||||||
|
$this->ui->setMediator($this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function printInfoAbout(string $user)
|
||||||
|
{
|
||||||
|
$this->ui->outputUserInfo($user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getUser(string $username): string
|
||||||
|
{
|
||||||
|
return $this->userRepository->getUserName($username);
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Before Width: | Height: | Size: 72 KiB After Width: | Height: | Size: 55 KiB |
Reference in New Issue
Block a user