美文网首页
c++装饰者模式

c++装饰者模式

作者: 一路向后 | 来源:发表于2021-02-13 20:27 被阅读0次

1.装饰者模式简介

   装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。

2.源码实现

#include <iostream>
#include <string>

using namespace std;

//抽象构件角色
class Phone
{
public:
    virtual void Show() = 0;
};

class iPhone : public Phone
{
public:
    iPhone(string strName) : m_strName(strName) {};
    ~iPhone() {};
    void Show()
    {
        cout << m_strName << endl;
    }

private:
    string m_strName;
};

class NokiaPhone : public Phone
{
public:
    NokiaPhone(string strName) : m_strName(strName) {};
    ~NokiaPhone() {};
    void Show()
    {
        cout << m_strName << endl;
    }

private:
    string m_strName;
};

//抽象装饰者角色
class Decorator : public Phone
{
public:
    Decorator(Phone *phone) : m_pPhone(phone) {};
    ~Decorator() {};
    virtual void Show()
    {
        m_pPhone->Show();
    }

private:
    Phone *m_pPhone;
};

class DecoratorPhoneA : public Decorator
{
public:
    DecoratorPhoneA(Phone *phone) : Decorator(phone) {};
    ~DecoratorPhoneA() {};
    void Show()
    {
        Decorator::Show();
        AddDecorator();     //添加新装饰
    }

private:
    inline void AddDecorator()
    {
        cout << "DecoratorPhoneA装饰" << endl;
    }
};

class DecoratorPhoneB : public Decorator
{
public:
    DecoratorPhoneB(Phone *phone) : Decorator(phone) {};
    ~DecoratorPhoneB() {};
    void Show()
    {
        Decorator::Show();
        AddDecorator();     //添加新装饰
    }

private:
    inline void AddDecorator()
    {
        cout << "DecoratorPhoneB装饰" << endl;
    }
};

int main(int argc, char **argv)
{
    //具体构件角色
    Phone *phone = new iPhone("iphone");

    if(phone == NULL)
    {
        return -1;
    }

    //具体装饰者角色
    Phone *pDePhone = new DecoratorPhoneA(phone);

    if(pDePhone == NULL)
    {
        delete phone;
        return -1;
    }

    pDePhone->Show();

    delete pDePhone;

    //具体装饰者角色
    pDePhone = new DecoratorPhoneB(phone);

    if(pDePhone == NULL)
    {
        delete phone;
        return -1;
    }

    pDePhone->Show();

    delete pDePhone;

    delete phone;

    return 0;
}

3.编译源码

$ g++ -o example example.cpp

4.运行及其结果

$ ./example
iphone
DecoratorPhoneA装饰
iphone
DecoratorPhoneB装饰

相关文章

  • c++装饰者模式

    1.装饰者模式简介    装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创...

  • 如何利用装饰者模式在不改变原有对象的基础上扩展功能

    目录 什么是装饰者模式 普通示例 装饰者模式示例 类图关系 装饰者模式使用场景 装饰者模式优点 装饰者模式缺点 什...

  • C++设计模式 ==> 装饰(者)模式

    简介 装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就...

  • 装饰者模式

    装饰者模式 装饰者模式和适配器模式对比 装饰者模式 是一种特别的适配器模式 装饰者与被装饰者都要实现同一个接口,主...

  • java IO 的知识总结

    装饰者模式 因为java的IO是基于装饰者模式设计的,所以要了解掌握IO 必须要先清楚什么事装饰者模式(装饰者模式...

  • 设计模式-装饰者模式

    装饰者模式概念: 装饰者模式又名包装(Wrapper)模式。装饰者模式以对客户端透明的方式扩展对象的功能,是继承关...

  • java - 装饰者模式

    装饰者模式 装饰者模式:动态将责任添加到对象上。如果需要扩展功能,装饰者提供了比继承更有弹性的解决方案。装饰者模式...

  • 设计模式之装饰者模式(Decorator Pattern)

    What: 装饰者模式又名包装(Wrapper)模式。装饰者模式动态地将责任附加到对象身上。若要扩展功能,装饰者提...

  • 装饰者(Decorator)模式

    装饰者(Decorator)模式装饰模式又名包装(Wrapper)模式。装饰模式是继承关系的一个替代方案。装饰模式...

  • 2、装饰者模式

    装饰者模式 一、基本概念 二、结构 三、案例1、装饰者模式案例2、JavaIO中使用装饰者模式 四、总结 一、基本...

网友评论

      本文标题:c++装饰者模式

      本文链接:https://www.haomeiwen.com/subject/ouqexltx.html