File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
Roadmap/24 - DECORADORES/php Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+
4+ interface Vehicle {
5+ public function wheels ();
6+ }
7+
8+ class Car implements Vehicle {
9+ public function wheels () {
10+ return 4 ;
11+ }
12+ }
13+
14+ class Bike implements Vehicle {
15+ public function wheels () {
16+ return 2 ;
17+ }
18+ }
19+
20+ class VehicleDecorator implements Vehicle {
21+ protected $ vehicle ;
22+
23+ public function __construct (Vehicle $ vehicle ) {
24+ $ this ->vehicle = $ vehicle ;
25+ }
26+
27+ public function wheels () {
28+ return $ this ->vehicle ->wheels ();
29+ }
30+ }
31+
32+ // Vamos a usar el patron decorador para definir ahora un tipo especial de coche que sol otien 2 puertas
33+ class Coupe extends VehicleDecorator {
34+ public function doors () {
35+ return ;
36+ }
37+ }
38+
39+ $ car = new Car ();
40+ $ bike = new Bike ();
41+ $ car ->wheels ();
42+ $ bike ->wheels ();
43+ $ carWithDoors = new Coupe ($ car );
44+ $ carWithDoors ->doors ();
45+ $ carWithDoors ->wheels ();
46+
47+
48+
49+
50+
You can’t perform that action at this time.
0 commit comments