-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediator.php
executable file
·111 lines (95 loc) · 3.22 KB
/
mediator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* Mediator — это поведенческий паттерн проектирования, который позволяет уменьшить связанность множества классов
* между собой, благодаря перемещению этих связей в один класс-посредник.
*
* https://refactoring.guru/ru/design-patterns/mediator
*
* Написать программу, используя паттерн Mediator:
*
* Консьерж сервис
* Можно давать задания на:
* - Вызов такси
* - Вызов мастера на дом
* - Доставку цветов
* и нотифицировать отправителя, не связывая их напрямую
*
* Решение на 107-й строке
*/
const FLOWER_SERVICE = 'flowerService';
const HOME_SERVICE = 'homeService';
const TAXI_SERVICE = 'taxiService';
abstract class UserAbstract
{
public string $name;
public string $address;
public function __construct(string $name, string $address)
{
$this->name = $name;
$this->address = $address;
}
abstract public function call(string $info);
}
class User extends UserAbstract
{
public function call(string $info)
{
echo "Got info from service: " . $info . PHP_EOL;
}
public function callToService(string $serviceName)
{
$mediator = new Mediator();
$mediator->executeService($serviceName, $this);
}
}
interface ServiceInterface
{
public function execute(UserAbstract $user): string;
}
class TaxiService implements ServiceInterface
{
public function execute(UserAbstract $user): string
{
return "executed taxiService for " . $user->name. PHP_EOL . 'Taxi to: ' . $user->address. PHP_EOL;
}
}
class HomeMasterService implements ServiceInterface
{
public function execute(UserAbstract $user): string
{
return "executed HomeMasterService for " . $user->name. PHP_EOL . 'Service by address: ' . $user->address. PHP_EOL;
}
}
class FlowersService implements ServiceInterface
{
public function execute(UserAbstract $user): string
{
return "executed FlowersService for " . $user->name. PHP_EOL . 'Delivery to: ' . $user->address. PHP_EOL;
}
}
class Mediator
{
public function executeService(string $serviceName, UserAbstract $user)
{
switch ($serviceName) {
case FLOWER_SERVICE:
$service = new FlowersService();
break;
case HOME_SERVICE:
$service = new HomeMasterService();
break;
case TAXI_SERVICE:
$service = new TaxiService();
break;
default:
throw new Exception('cant find service');
}
$serviceResponse = $service->execute($user);
$user->call($serviceResponse);
}
}
# Client code
$user1 = new User('Vasya', 'podval');
$user2 = new User('Mykola', 'cherdak');
$user1->callToService(FLOWER_SERVICE); // Got info from service: executed FlowersService for Vasya. Delivery to: podval
$user2->callToService(TAXI_SERVICE); // Got info from service: executed taxiService for Mykola. Taxi to: cherdak