@@ -32,19 +32,62 @@ public function wheels() {
3232 // Vamos a usar el patron decorador para definir ahora un tipo especial de coche que sol otien 2 puertas
3333 class Coupe extends VehicleDecorator {
3434 public function doors () {
35- return ;
35+ return 2 ;
3636 }
3737 }
3838
3939 $ car = new Car ();
4040 $ 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" ;
4343 $ 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+ }
4684
85+ $ suma = new Suma (2 , 2 );
86+ $ sumaContada = new ContadorDeLlamadas ($ suma );
4787
88+ echo $ sumaContada ->ejecutar () . "\n" ;
89+ echo $ sumaContada ->ejecutar () . "\n" ;
90+ echo $ sumaContada ->ejecutar () . "\n" ;
4891
4992
5093
0 commit comments