You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
echo"\nEl area del rectangulo es: ". $shape->width * $shape->height;
29
+
} elseif ($shapeinstanceof Circle) {
30
+
echo"\nEl area del circulo es: ". pi() * $shape->radius * $shape->radius;
31
+
}
32
+
}
33
+
}
34
+
}
35
+
36
+
$shapes = [
37
+
newRectangle(5, 10),
38
+
newCircle(7)
39
+
];
40
+
41
+
echo"Ejercicio básico (sin cumplir OCP)";
42
+
$calculator = newAreaCalculator();
43
+
echo$calculator->calculate($shapes);
44
+
45
+
// El problema de esta solución es que si ahroa creamos una clase triangle (por ejemplo) y queremos calcualr su área, debemos modificar AreaCalculator para tener en cuenta la nueva forma
46
+
47
+
interface Shape {
48
+
publicfunctionarea();
49
+
}
50
+
51
+
class Rectangle2 implements Shape {
52
+
private$width;
53
+
private$height;
54
+
55
+
publicfunction__construct($width, $height) {
56
+
$this->width = $width;
57
+
$this->height = $height;
58
+
}
59
+
60
+
publicfunctionarea() {
61
+
return$this->width * $this->height;
62
+
}
63
+
}
64
+
65
+
class Circle2 implements Shape {
66
+
private$radius;
67
+
68
+
publicfunction__construct($radius) {
69
+
$this->radius = $radius;
70
+
}
71
+
72
+
publicfunctionarea() {
73
+
returnpi() * $this->radius * $this->radius;
74
+
}
75
+
}
76
+
77
+
class AreaCalculator2 {
78
+
publicfunctioncalculate($shapes) {
79
+
80
+
foreach ($shapesas$shape) {
81
+
echo"\nEl área de la forma es: ". $shape->area();
82
+
}
83
+
}
84
+
}
85
+
86
+
$shapes = [
87
+
newRectangle2(5, 10),
88
+
newCircle2(7)
89
+
];
90
+
91
+
echo"\nEjercicio básico (cumpliendo OCP)";
92
+
$calculator = newAreaCalculator2();
93
+
echo$calculator->calculate($shapes);
94
+
95
+
// Con este enfoque, la clase Triangle tendría su propia definición de como calcular el área y AreaCalculator2 no se tendría que modificar
96
+
97
+
// Ejercicio Extra
98
+
99
+
interface Operation {
100
+
publicfunctioncalculate ($a, $b);
101
+
}
102
+
103
+
class Sum implements Operation {
104
+
publicfunctioncalculate($a, $b) {
105
+
return$a + $b;
106
+
}
107
+
}
108
+
109
+
class Substract implements Operation {
110
+
publicfunctioncalculate($a, $b) {
111
+
return$a - $b;
112
+
}
113
+
}
114
+
115
+
class Multiply implements Operation {
116
+
publicfunctioncalculate($a, $b) {
117
+
return$a * $b;
118
+
}
119
+
}
120
+
121
+
class Divition implements Operation{
122
+
publicfunctioncalculate($a, $b) {
123
+
return$a / $b;
124
+
}
125
+
}
126
+
127
+
class Calculator {
128
+
publicfunctioncalculate($operation, $a, $b) {
129
+
return$operation->calculate($a, $b);
130
+
}
131
+
}
132
+
133
+
$calculator = newCalculator();
134
+
echo"\nEjercicio extra";
135
+
echo"\nLa suma de 5 y 10 es: ". $calculator->calculate(newSum(), 5, 10);
136
+
echo"\nLa resta de 5 y 10 es: ". $calculator->calculate(newSubstract(), 5, 10);
137
+
echo"\nLa multiplicación de 5 y 10 es: ". $calculator->calculate(newMultiply(), 5, 10);
138
+
echo"\nLa división de 5 y 10 es: ". $calculator->calculate(newDivition(), 5, 10);
139
+
140
+
// A continuación vamos a añadir la operación potencia.
141
+
142
+
class Power implements Operation {
143
+
publicfunctioncalculate($a, $b) {
144
+
returnpow($a, $b);
145
+
}
146
+
}
147
+
148
+
echo"\nLa potencia de 5 y 10 es: ". $calculator->calculate(newPower(), 5, 10);
149
+
150
+
// Como vemos, no hemos tenido que modificar nada en la clase Calculator
0 commit comments