[PHP] 装饰器模式-结构型设计模式
发布时间:2021-05-21 06:55:23 所属栏目:大数据 来源: https://www.jb51.cc
导读:动态地为类的实例添加功能,一层一层的套功能 先定义好接口 interface Booking{ public function getDescription(): string ;} ? 这个就是装饰器实现了Booking,通过构造函数传递Booking对象进来 abstract class BookingDecorator implements Booking{ protec
动态地为类的实例添加功能,一层一层的套功能 先定义好接口 interface Booking{ public function getDescription(): string; } ?
abstract class BookingDecorator implements Booking{ protected Booking $booking; public function __construct(Booking $booking) { $this->booking = $booking; } } ? 这个类直接实现Booking DoubleRoomBooking implements Booking { { return 'double room'; } } ?
WiFi extends BookingDecorator{ { return $this->booking->getDescription() . with wifi; } } ExtraBed extends BookingDecorator { public function calculatePrice(): intthis->booking->calculatePrice() + self::PRICE; } { with extra bed; } } ?
(编辑:北几岛) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |