@@ -32,19 +32,62 @@ public function wheels() {
32
32
// Vamos a usar el patron decorador para definir ahora un tipo especial de coche que sol otien 2 puertas
33
33
class Coupe extends VehicleDecorator {
34
34
public function doors () {
35
- return ;
35
+ return 2 ;
36
36
}
37
37
}
38
38
39
39
$ car = new Car ();
40
40
$ bike = new Bike ();
41
- $ car ->wheels ();
42
- $ bike ->wheels ();
41
+ echo " Un coche tiene " . $ car ->wheels (). " ruedas \n" ;
42
+ echo " Una bicicleta tiene " . $ bike ->wheels (). " ruedas \n" ;
43
43
$ carWithDoors = new Coupe ($ car );
44
- $ carWithDoors ->doors ();
45
- $ carWithDoors ->wheels ();
44
+ echo "Prueba de uso de decorador \n" ;
45
+ echo "Un coche modelo Coupe tiene " .$ carWithDoors ->wheels (). " ruedas pero solo tiene " .$ carWithDoors ->doors ()." puertas \n" ;
46
+
47
+ // Ejercicio extra
48
+
49
+ echo "\n\nEjercicio extra \n" ;
50
+
51
+
52
+ interface Funcion {
53
+ public function ejecutar ();
54
+ }
55
+
56
+ class Suma implements Funcion {
57
+ private $ a ;
58
+ private $ b ;
59
+
60
+ public function __construct ($ a , $ b ) {
61
+ $ this ->a = $ a ;
62
+ $ this ->b = $ b ;
63
+ }
64
+
65
+ public function ejecutar () {
66
+ return $ this ->a + $ this ->b ;
67
+ }
68
+ }
69
+
70
+ class ContadorDeLlamadas implements Funcion {
71
+ private $ funcion ;
72
+ private static $ contador = 0 ;
73
+
74
+ public function __construct (Funcion $ funcion ) {
75
+ $ this ->funcion = $ funcion ;
76
+ }
77
+
78
+ public function ejecutar () {
79
+ self ::$ contador ++;
80
+ echo "La función ha sido llamada " . self ::$ contador . " veces. \n" ;
81
+ return $ this ->funcion ->ejecutar ();
82
+ }
83
+ }
46
84
85
+ $ suma = new Suma (2 , 2 );
86
+ $ sumaContada = new ContadorDeLlamadas ($ suma );
47
87
88
+ echo $ sumaContada ->ejecutar () . "\n" ;
89
+ echo $ sumaContada ->ejecutar () . "\n" ;
90
+ echo $ sumaContada ->ejecutar () . "\n" ;
48
91
49
92
50
93
0 commit comments