Skip to content

Commit 64e21e8

Browse files
author
Dominik Liebler
committed
PHP7 Mediator
1 parent a6b09d6 commit 64e21e8

File tree

7 files changed

+36
-82
lines changed

7 files changed

+36
-82
lines changed

Behavioral/Mediator/Colleague.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* Colleague is an abstract colleague who works together but he only knows
7-
* the Mediator, not other colleague.
7+
* the Mediator, not other colleagues
88
*/
99
abstract class Colleague
1010
{
@@ -13,21 +13,13 @@ abstract class Colleague
1313
*
1414
* @var MediatorInterface
1515
*/
16-
private $mediator;
16+
protected $mediator;
1717

1818
/**
19-
* @param MediatorInterface $medium
19+
* @param MediatorInterface $mediator
2020
*/
21-
public function __construct(MediatorInterface $medium)
21+
public function setMediator(MediatorInterface $mediator)
2222
{
23-
// in this way, we are sure the concrete colleague knows the mediator
24-
$this->mediator = $medium;
25-
}
26-
27-
// for subclasses
28-
29-
protected function getMediator()
30-
{
31-
return $this->mediator;
23+
$this->mediator = $mediator;
3224
}
3325
}

Behavioral/Mediator/Mediator.php

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,54 @@
33
namespace DesignPatterns\Behavioral\Mediator;
44

55
/**
6-
* Mediator is the concrete Mediator for this design pattern.
7-
* In this example, I have made a "Hello World" with the Mediator Pattern.
6+
* Mediator is the concrete Mediator for this design pattern
7+
*
8+
* In this example, I have made a "Hello World" with the Mediator Pattern
89
*/
910
class Mediator implements MediatorInterface
1011
{
1112
/**
1213
* @var Subsystem\Server
1314
*/
14-
protected $server;
15+
private $server;
1516

1617
/**
1718
* @var Subsystem\Database
1819
*/
19-
protected $database;
20+
private $database;
2021

2122
/**
2223
* @var Subsystem\Client
2324
*/
24-
protected $client;
25+
private $client;
2526

2627
/**
27-
* @param Subsystem\Database $db
28-
* @param Subsystem\Client $cl
29-
* @param Subsystem\Server $srv
28+
* @param Subsystem\Database $database
29+
* @param Subsystem\Client $client
30+
* @param Subsystem\Server $server
3031
*/
31-
public function setColleague(Subsystem\Database $db, Subsystem\Client $cl, Subsystem\Server $srv)
32+
public function __construct(Subsystem\Database $database, Subsystem\Client $client, Subsystem\Server $server)
3233
{
33-
$this->database = $db;
34-
$this->server = $srv;
35-
$this->client = $cl;
34+
$this->database = $database;
35+
$this->server = $server;
36+
$this->client = $client;
37+
38+
$this->database->setMediator($this);
39+
$this->server->setMediator($this);
40+
$this->client->setMediator($this);
3641
}
3742

38-
/**
39-
* make request.
40-
*/
4143
public function makeRequest()
4244
{
4345
$this->server->process();
4446
}
4547

46-
/**
47-
* query db.
48-
*
49-
* @return mixed
50-
*/
51-
public function queryDb()
48+
public function queryDb(): string
5249
{
5350
return $this->database->getData();
5451
}
5552

5653
/**
57-
* send response.
58-
*
5954
* @param string $content
6055
*/
6156
public function sendResponse($content)

Behavioral/Mediator/MediatorInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* MediatorInterface is a contract for the Mediator
7-
* This interface is not mandatory but it is better for LSP concerns.
7+
* This interface is not mandatory but it is better for Liskov substitution principle concerns.
88
*/
99
interface MediatorInterface
1010
{
@@ -16,12 +16,12 @@ interface MediatorInterface
1616
public function sendResponse($content);
1717

1818
/**
19-
* makes a request.
19+
* makes a request
2020
*/
2121
public function makeRequest();
2222

2323
/**
24-
* queries the DB.
24+
* queries the DB
2525
*/
2626
public function queryDb();
2727
}

Behavioral/Mediator/Subsystem/Client.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,16 @@
55
use DesignPatterns\Behavioral\Mediator\Colleague;
66

77
/**
8-
* Client is a client that make request et get response.
8+
* Client is a client that makes requests and gets the response response.
99
*/
1010
class Client extends Colleague
1111
{
12-
/**
13-
* request.
14-
*/
1512
public function request()
1613
{
17-
$this->getMediator()->makeRequest();
14+
$this->mediator->makeRequest();
1815
}
1916

20-
/**
21-
* output content.
22-
*
23-
* @param string $content
24-
*/
25-
public function output($content)
17+
public function output(string $content)
2618
{
2719
echo $content;
2820
}

Behavioral/Mediator/Subsystem/Database.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,9 @@
44

55
use DesignPatterns\Behavioral\Mediator\Colleague;
66

7-
/**
8-
* Database is a database service.
9-
*/
107
class Database extends Colleague
118
{
12-
/**
13-
* @return string
14-
*/
15-
public function getData()
9+
public function getData(): string
1610
{
1711
return 'World';
1812
}

Behavioral/Mediator/Subsystem/Server.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44

55
use DesignPatterns\Behavioral\Mediator\Colleague;
66

7-
/**
8-
* Server serves responses.
9-
*/
107
class Server extends Colleague
118
{
12-
/**
13-
* process on server.
14-
*/
159
public function process()
1610
{
17-
$data = $this->getMediator()->queryDb();
18-
$this->getMediator()->sendResponse("Hello $data");
11+
$data = $this->mediator->queryDb();
12+
$this->mediator->sendResponse(sprintf("Hello %s", $data));
1913
}
2014
}

Behavioral/Mediator/Tests/MediatorTest.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,14 @@
77
use DesignPatterns\Behavioral\Mediator\Subsystem\Database;
88
use DesignPatterns\Behavioral\Mediator\Subsystem\Server;
99

10-
/**
11-
* MediatorTest tests hello world.
12-
*/
1310
class MediatorTest extends \PHPUnit_Framework_TestCase
1411
{
15-
protected $client;
16-
17-
protected function setUp()
18-
{
19-
$media = new Mediator();
20-
$this->client = new Client($media);
21-
$media->setColleague(new Database($media), $this->client, new Server($media));
22-
}
23-
2412
public function testOutputHelloWorld()
2513
{
26-
// testing if Hello World is output :
14+
$client = new Client();
15+
new Mediator(new Database(), $client, new Server());
16+
2717
$this->expectOutputString('Hello World');
28-
// as you see, the 3 components Client, Server and Database are totally decoupled
29-
$this->client->request();
30-
// Anyway, it remains complexity in the Mediator that's why the pattern
31-
// Observer is preferable in mnay situations.
18+
$client->request();
3219
}
3320
}

0 commit comments

Comments
 (0)