先定义好接口
interface Booking{ public function getDescription(): string; }
这个就是装饰器实现了Booking,通过构造函数传递Booking对象进来
abstract class BookingDecorator implements Booking{ protected Booking $booking; public function __construct(Booking $booking) { $this->booking = $booking; } }
这个类直接实现Booking
DoubleRoomBooking implements Booking { { return 'double room'; } }
这个类继承了装饰器,实现了Booking
WiFi extends BookingDecorator{ { return $this->booking->getDescription() . with wifi; } } ExtraBed extends BookingDecorator { public function calculatePrice(): intthis->booking->calculatePrice() + self::PRICE; } { with extra bed; } }
$booking = new DoubleRoomBooking();
//继承装饰器的可以一层层套
$booking = new WiFi($booking);
$booking = new ExtraBed($booking);