Skip to content

Commit 77550bd

Browse files
author
Miguel Ángel Delgado
committed
#24 - php
1 parent 80d137c commit 77550bd

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+

0 commit comments

Comments
 (0)