《C++新经典设计模式》之第6章 装饰器模式

《C++新经典设计模式》之第6章 装饰器模式

装饰器模式.cpp
#include <iostream>
#include <memory>
using namespace std;

// 通过组装方式增强类的功能(动态增加新功能)
// 4种角色
// Control(抽象构件),定义必需接口
// ListCtrl(具体构件),实现接口且用装饰器可给该构件增加额外方法
// Decorator(抽象装饰器类),扩展接口,用于装饰
// BorderDec、VerScrollBarDec、HorScrollBarDec(具体装饰器类),增加新方法扩充构件能力

// 组合复用原则
// 组合优于继承

namespace ns1
{
    class Control
    {
    public:
        virtual ~Control() {}

    public:
        virtual void draw() const = 0;
    };

    class ListCtrl : public Control
    {
    public:
        void draw() const override { cout << "Draw Common List Controls!" << endl; }
    };

    class Decorator : public Control
    {
        shared_ptr<Control> m_control;

    public:
        Decorator(const shared_ptr<Control> &tmpctrl) : m_control(tmpctrl) {}
        void draw() const override { m_control->draw(); }
    };

    class BorderDec : public Decorator
    {
        void drawBorder() const { cout << "bound box!" << endl; }

    public:
        BorderDec(const shared_ptr<Control> &tmpctrl) : Decorator(tmpctrl) {}
        void draw() const override
        {
            Decorator::draw();
            drawBorder();
        }
    };

    class VerScrollBarDec : public Decorator
    {
        void drawVerScrollBar() const { cout << "Draw vertical scroll bar!" << endl; }

    public:
        VerScrollBarDec(const shared_ptr<Control> &tmpctrl) : Decorator(tmpctrl) {}
        void draw() const override
        {
            Decorator::draw();
            drawVerScrollBar();
        }
    };

    class HorScrollBarDec : public Decorator
    {
        void drawHorScrollBar() const { cout << "Draw horizontal scroll bar!" << endl; }

    public:
        HorScrollBarDec(const shared_ptr<Control> &tmpctrl) : Decorator(tmpctrl) {}
        void draw() const override
        {
            Decorator::draw();
            drawHorScrollBar();
        }
    };
}

namespace ns2
{
    class Beverage // 抽象的饮料类
    {
    public:
        virtual ~Beverage() {}
        virtual int getprice() const = 0; // 获取价格
    };

    class FruitBeverage : public Beverage // 水果饮料类
    {
    public:
        int getprice() const override { return 10; }
    };

    class Decorator : public Beverage // 抽象的装饰器类
    {
        shared_ptr<Beverage> m_pbvg;

    public:
        Decorator(const shared_ptr<Beverage> &tmpbvg) : m_pbvg(tmpbvg) {}
        int getprice() const override { return m_pbvg->getprice(); }
    };

    class SugarDec : public Decorator // 具体的“砂糖”装饰器类
    {
    public:
        SugarDec(const shared_ptr<Beverage> &tmpbvg) : Decorator(tmpbvg) {}
        int getprice() const override { return Decorator::getprice() + 1; }
    };

    class MilkDec : public Decorator // 具体的“牛奶”装饰器类
    {
    public:
        MilkDec(const shared_ptr<Beverage> &tmpbvg) : Decorator(tmpbvg) {}
        int getprice() const override { return Decorator::getprice() + 2; }
    };

    class BubbleDec : public Decorator // 具体的“珍珠”装饰器类
    {
    public:
        BubbleDec(const shared_ptr<Beverage> &tmpbvg) : Decorator(tmpbvg) {}
        int getprice() const override { return Decorator::getprice() + 2; }
    };
}

namespace ns3
{
    class Shape
    {
    public:
        virtual ~Shape() = default;
        virtual void draw() const = 0;
    };

    class Rectangle : public Shape
    {
    public:
        void draw() const override { cout << "Shape: Rectangle" << endl; }
    };

    class Circle : public Shape
    {
    public:
        void draw() const override { cout << "Shape: Circle" << endl; }
    };

    class ShapeDecorator : public Shape
    {
    protected:
        shared_ptr<Shape> decoratedShape;

    public:
        ShapeDecorator(const shared_ptr<Shape> &s) : decoratedShape(s) {}
        void draw() const override { decoratedShape->draw(); }
    };

    class RedShapeDecorator : public ShapeDecorator
    {
        void setRedBorder() const { cout << "Border Color: Red" << endl; }

    public:
        RedShapeDecorator(const shared_ptr<Shape> &s) : ShapeDecorator(s) {}

        void draw() const override
        {
            // ShapeDecorator::draw();
            decoratedShape->draw();
            setRedBorder();
        }
    };
}

int main()
{
#if 0
    using namespace ns1;
    //(1)创建一个又带边框,又带垂直滚动条的列表控件
    shared_ptr<Control> plistctrl(new ListCtrl());                       // 普通列表控件
    shared_ptr<Control> plistctrl_b(new BorderDec(plistctrl));           // 带边框的列表控件
    shared_ptr<Control> plistctrl_b_v(new VerScrollBarDec(plistctrl_b)); // 带垂直滚动条又带边框的列表控件
    plistctrl_b_v->draw();
    cout << "-------------------------------" << endl;
    //(2)创建一个只带水平滚动条的列表控件
    shared_ptr<Control> plistctrl2(new ListCtrl());                    // 普通列表控件
    shared_ptr<Control> plistctrl2_h(new HorScrollBarDec(plistctrl2)); // 带水平滚动条的列表控件
    plistctrl2_h->draw();
#endif

#if 0
    using namespace ns2;
    shared_ptr<Beverage> pfruit(new FruitBeverage());                           // 单纯水果饮料,10元
    shared_ptr<Beverage> pfruit_addbubb(new BubbleDec(pfruit));                 // 增加珍珠,增加2元
    shared_ptr<Beverage> pfruit_addbubb_addsugar(new SugarDec(pfruit_addbubb)); // 增加砂糖,增加1元
    cout << "last price: " << pfruit_addbubb_addsugar->getprice() << endl;      // 最终价格
#endif

#if 1
    using namespace ns3;
    shared_ptr<Shape> circle(new Circle());
    cout << "Circle with normal border" << endl;
    circle->draw();

    shared_ptr<Shape> redCircle(new RedShapeDecorator(circle));
    cout << "\nCircle of red border" << endl;
    redCircle->draw();

    shared_ptr<Shape> rectange(new Rectangle());
    shared_ptr<Shape> redRectangle(new RedShapeDecorator(rectange));
    cout << "\nRectangle of red border" << endl;
    redRectangle->draw();
#endif

    cout << "Over!\n";
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: "C经典-设计模式"是一本非常重要的书籍,它详细介绍了23种常用的设计模式设计模式是指在软件开发中,经过实践证明有效的解决方案。这本书提供了大量的示例和实践案例,帮助读者理解并应用这些设计模式。 这本书的特点之一是提供了丰富的图示,通过图示直观地展示了设计模式的结构和关系。这对于理解和记忆设计模式非常有帮助。 这本书从简单到复杂地介绍了各种不同类型的设计模式,包括创建型、结构型和行为型设计模式。每种设计模式都包括了详细的定义、应用场景、解决方案和与其他模式的关系。这样的组织结构使得读者可以快速地了解并选择适合自己项目需求的设计模式。 "C经典-设计模式"还提供了大量的实例代码和案例分析,帮助读者更好地理解和应用设计模式。这些案例覆盖了各种不同的编程语言和开发环境,使得读者可以从不同的角度去理解设计模式。 总的来说,"C经典-设计模式"是一本极具价值的书籍,无论是对于初学者还是有经验的开发者都具有很大的帮助。通过学习和应用这些设计模式,读者可以提高软件开发的质量和效率,使得自己的代码更加灵活、可维护和可扩展。 ### 回答2: "C经典设计模式pdf" 是指《C++经典设计模式》这本书的电子版PDF文件。这本书是一本关于C++设计模式经典著作,对于C++开发者学习和应用设计模式非常有帮助。通过阅读这本书的PDF版本,可以更方便地学习和参考其中的内容。 该PDF文件包含了本书的全部内容,包括理论介绍、具体的设计模式实现案例和相关的示意图等。使用该PDF文件,可以将书籍随身携带,随时进行学习和查阅。无论是初学者还是经验丰富的开发者都可以通过研读该书,深入了解各种设计模式的原理和使用方法。 设计模式是一种解决常见软件设计问题的经验总结,它们提供了一套通用且可重复使用的解决方案。通过应用设计模式,我们可以使得我们的代码更加灵活、可扩展和易于维护。在C++开发中使用设计模式,不仅可以提升代码质量和性能,还可以提高开发效率和团队协作能力。 《C++经典设计模式》是一本深入浅出的教材,适合各个层次的C++开发者阅读。无论你是刚入门的初学者,还是已经有一定经验的开发者,都可以从书中学到很多有用的知识和技巧。通过阅读该书的PDF版,可以更便捷地学习和实践设计模式,提升自身的软件设计和编码能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值