zoukankan      html  css  js  c++  java
  • [PHP] 装饰器模式-结构型设计模式

    动态地为类的实例添加功能,一层一层的套功能

    先定义好接口

    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

    class DoubleRoomBooking implements Booking
    {
        public function getDescription(): string{
            return 'double room';
        }
    }


    这个类继承了装饰器,实现了Booking

    class WiFi extends BookingDecorator{
        public function getDescription(): string
        {
            return $this->booking->getDescription() . ' with wifi';
        }
    }
    class ExtraBed extends BookingDecorator
    {
        public function calculatePrice(): int
        {
            return $this->booking->calculatePrice() + self::PRICE;
        }
        public function getDescription(): string {
            return $this->booking->getDescription() . ' with extra bed';
        }
    }


    $booking = new DoubleRoomBooking();
    //继承装饰器的可以一层层套
    $booking = new WiFi($booking);
    $booking = new ExtraBed($booking);

  • 相关阅读:
    boost::ptree;boost::xml_parser
    boost::array
    boost::timer
    boost::gregorian日期
    boost::algorithm/string.hpp
    boost::lexical_cast
    QT::绘图
    QT::透明
    centos上freefilesync与定时任务
    centos上安装freefilesync工具配置说明
  • 原文地址:https://www.cnblogs.com/taoshihan/p/13836582.html
Copyright © 2011-2022 走看看