This is a simple implementation of the decorator design pattern in PHP.
- Clone this repository
- Run
composer install
- Run
php src/main.php
classDiagram
BanhMi --|>BanhMiInterface
BanhMiDecorator --|> BanhMiInterface
DuaLeo --|> BanhMiDecorator
ChaLua --|> BanhMiDecorator
TuongOt --|> BanhMiDecorator
Thit --|> BanhMiDecorator
class BanhMi {
+Cost() float
}
class BanhMiInterface {
+Cost() float
}
class BanhMiDecorator {
-BanhMiInterface banhMi
+BanhMiDecorator(BanhMiInterface wrappee)
+Cost() float
}
$firstBanhMi = new Thit(
new TuongOt(
new DuaLeo(
new ChaLua(
new BanhMi()))));
echo "Cost of first banh mi: " . $firstBanhMi->cost() . " VND" . PHP_EOL;
$secondBanhMi = new Thit(
new TuongOt(
new DuaLeo(
new BanhMi()
)
)
);
echo "Cost of second banh mi: " . $secondBanhMi->cost() . " VND" . PHP_EOL;
Cost of first banh mi: 12000 VND
Cost of second banh mi: 9500 VND