zoukankan      html  css  js  c++  java
  • 设计模式 之 《装饰模式》

    #ifndef __DECORATOR__
    #define __DECORATOR__
    
    #include <string>
    #include <iostream>
    using namespace std;
    
    //
    class Person
    {
    public:
        Person(string strName)
        {
            m_strName = strName;
        }
    
        Person(){}
    
        virtual void show()
        {
            cout<<"装扮的是:"<<m_strName<<endl;
        }
    
    private:
        string m_strName;
    
    };
    
    //装饰类
    class Finery : public Person
    {
    protected:
        Person* m_component;
    public:
        void Decorator(Person* component)
        {
            m_component = component;
        }
        virtual void show()
        {
            m_component->show();
        }
    };
    
    class TShirts : public Finery
    {
    public:
        virtual void show()
        {
            cout<<"T Shirts"<<endl;
            m_component->show();
        }
    
    };
    
    class BigTrouser : public Finery
    {
    public:
        virtual void show()
        {
            cout<<"Big Trouser"<<endl;
            m_component->show();
        }
    };
    
    
    #endif //__DECORATOR__
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        Person* person = new Person("张三");
        
        BigTrouser* bt = new BigTrouser();
        TShirts* ts = new TShirts();
        bt->Decorator(person);
        ts->Decorator(bt);
        ts->show();
    
        return 0;
    }
  • 相关阅读:
    mysql逻辑架构
    delete与truncate的区别
    mycat
    mycat
    MyCat数据库中间件
    mysql主从复制
    docker学习笔记之快速安装
    linux学习笔记之CentOS7系统快速安装
    Redis学习笔记
    双绞线的种类与型号
  • 原文地址:https://www.cnblogs.com/MrGreen/p/3383335.html
Copyright © 2011-2022 走看看