forked from delineas/principios-solid-para-novatos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
4-interface-segregation.php
121 lines (104 loc) · 2.58 KB
/
4-interface-segregation.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
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Violación del principio
*
* $cowSpeed = new CowSpeed(10, 60);
* $cowSpeed->airSpeed(); // Las vacas no vuelan
* $DuckSpeed = new DuckSpeed(10, 60);
* $DuckSpeed->airSpeed();
*
*/
interface AnimalSpeedInterface
{
public function groundSpeed();
public function airSpeed();
}
class CowSpeed implements AnimalSpeedInterface
{
protected $meters;
protected $seconds;
protected $groundFactor;
protected $airFactor;
public function __construct($meters, $seconds)
{
$this->meters = $meters;
$this->seconds = $seconds;
$this->groundFactor = 1;
$this->airFactor = null;
}
public function groundSpeed()
{
return $this->groundFactor * ($this->meters / $this->seconds);
}
public function airSpeed()
{
return null;
}
}
class DuckSpeed implements AnimalSpeedInterface
{
protected $meters;
protected $seconds;
protected $groundFactor;
protected $airFactor;
public function __construct($meters, $seconds)
{
$this->meters = $meters;
$this->seconds = $seconds;
$this->groundFactor = 0.01;
$this->airFactor = 1.2;
}
public function groundSpeed()
{
return $this->groundFactor * ($this->meters / $this->seconds);
}
public function airSpeed()
{
return $this->airFactor * ($this->meters / $this->seconds);
}
}
/**
* Una interfaz intermedia que discrimine el cáclulo
*
* $cowSpeed = new CowSpeedRefactor(10, 60);
* $cowSpeed->calculateSpeed(); // Las vacas no vuelan
* $DuckSpeed = new DuckSpeedRefactor(10, 60);
* $DuckSpeed->calculateSpeed();
*/
interface AnimalSpeedInterfaceRefactor
{
public function calculateSpeed();
}
class CowSpeedRefactor implements AnimalSpeedInterfaceRefactor
{
protected $meters;
protected $seconds;
protected $groundFactor;
public function __construct($meters, $seconds)
{
$this->meters = $meters;
$this->seconds = $seconds;
$this->groundFactor = 1;
}
public function calculateSpeed()
{
return $this->groundFactor * ($this->meters / $this->seconds);
}
}
class DuckSpeedRefactor implements AnimalSpeedInterfaceRefactor
{
protected $meters;
protected $seconds;
protected $airFactor;
public function __construct($meters, $seconds)
{
$this->meters = $meters;
$this->seconds = $seconds;
$this->groundFactor = 0.01;
$this->airFactor = 1.2;
}
public function calculateSpeed()
{
return $this->airFactor * ($this->meters / $this->seconds);
}
}