-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbad_design.php
More file actions
96 lines (81 loc) · 2.04 KB
/
bad_design.php
File metadata and controls
96 lines (81 loc) · 2.04 KB
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
<?php
/*
* A large interfsce called `Worker` with methods for all kind of tasks:
* `work`, `manageProject`, `writeCode`
*/
interface Worker
{
public function work();
public function manageProjects();
public function writeCode();
}
/*
* A `Developer` needs to `work` and `writeCode` but
* does not manage projects. But still we are forcing
* developer to implement manageProjects() method.
*
*/
class Developer implements Worker
{
public function work() : void
{
echo 'Developer is working...' . PHP_EOL;
}
public function writeCode() : void
{
echo "Developer is writing code..." . PHP_EOL;
}
// Problem: Developer doesn't manage projects
public function manageProjects() : Exception
{
throw new \Exception('Developer does not manage projects...' . PHP_EOL);
}
}
/*
* A `Manager` needs to `work` and `manageProjects` but
* does not write code. But still we are forcing manager
* to implement writeCode() method.
*
*/
class Manager implements Worker
{
public function work() : void
{
echo 'Manager is working...' . PHP_EOL;
}
public function manageProjects() : void
{
echo 'Manager is managing projects...' . PHP_EOL;
}
// Problem: Manager doesn't write code
public function writeCode() : Exception
{
throw new \Exception('Manager does not write code.' . PHP_EOL);
}
}
// Usages - Developer
try {
$developer = new Developer();
$developer->work();
$developer->writeCode();
$developer->manageProjects(); // throw exception
} catch (\Throwable $th) {
print $th->getMessage();
}
// Usages - Manager
try {
$manager = new Manager();
$manager->work();
$manager->manageProjects();
$manager->writeCode(); // throw exception
} catch (\Throwable $th){
print $th->getMessage();
}
/* Output::
Developer is working...
Developer is writing code...
Developer does not manage projects...
Manager is working...
Manager is managing projects...
Manager does not write code.
*/