1. 程式人生 > >設計模式——6.裝飾模式

設計模式——6.裝飾模式

結構 key 執行 ram sta std ddb 類繼承 分享圖片

裝飾模式(Decorator)

動態的為一個對象增加額外的職責,為對象增加功能時,使用裝飾模式比單純的子類繼承要更加的靈活。

裝飾模式結構圖:

技術分享圖片

C++

BaseClass對象抽象基類:


    //file: BaseClass.h
    #pragma once
    class BaseClass
    {
    public:
        BaseClass();
        virtual ~BaseClass();
    
        virtual void operation();
    };


    //file: BaseClass.cpp
    #include "pch.h"
    #include "BaseClass.h"
    
    BaseClass::BaseClass() {}
    
    BaseClass::~BaseClass() {}
    
    void BaseClass::operation() {}

ConcreteClass具體對象實現類:


    //file: ConcreteClass.h
    #pragma once
    #include "BaseClass.h"
    
    class ConcreteClass : public BaseClass
    {
    public:
        ConcreteClass();
        virtual ~ConcreteClass();
    
        void operation();
    };


    //file: ConcreteClass.cpp
    #include "pch.h"
    #include "ConcreteClass.h"
    #include <iostream>
    
    ConcreteClass::ConcreteClass() {}
    
    ConcreteClass::~ConcreteClass() {}
    
    void ConcreteClass::operation()
    {
        std::cout << "ConcreteComponent's normal operation!" << std::endl;
    }

Decorator裝飾抽象類:


    //file: Decorator.h
    #pragma once
    #include "BaseClass.h"
    
    class Decorator : public BaseClass
    {
    public:
        Decorator(BaseClass *pcom);
        virtual ~Decorator();
        
        void operation();
    
    private:
        BaseClass *m_BaseClass;
    };
    
    //file: Decorator.cpp
    #include "pch.h"
    #include "Decorator.h"
    
    Decorator::Decorator(BaseClass *pcom) : m_BaseClass(pcom) {}
    
    Decorator::~Decorator() {}
    
    void Decorator::operation()
    {
        m_BaseClass->operation();
    }

ConcreteDecoratorA具體裝飾是實現類:


    //file: ConcreteDecoratorA.h
    #pragma once
    #include "Decorator.h"
    
    class ConcreteDecoratorA : public Decorator
    {
    public:
        ConcreteDecoratorA(BaseClass *pcom);
        virtual ~ConcreteDecoratorA();
    
        virtual void operation();
        void addBehavior();
    };

    //file: ConcreteDecoratorA.cpp
    #include "pch.h"
    #include "ConcreteDecoratorA.h"
    #include <iostream>
    
    ConcreteDecoratorA::ConcreteDecoratorA(BaseClass *pcom) : Decorator(pcom) {}
    
    ConcreteDecoratorA::~ConcreteDecoratorA() {}
    
    void ConcreteDecoratorA::addBehavior()
    {
        std::cout << "addBehavior AAAA" << std::endl;
    }
    
    void ConcreteDecoratorA::operation()
    {
        Decorator::operation();
        addBehavior();
    }

客戶端代碼:


    //file: DecoratorPattern.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
    #include "pch.h"
    #include "ConcreteClass.h"
    #include "ConcreteDecoratorA.h"
    #include "ConcreteDecoratorB.h"
    #include <iostream>
    using namespace std;
    
    int main()
    {
        ConcreteClass *p = new ConcreteClass();
    
        BaseClass *pa = new ConcreteDecoratorA(p);
        pa->operation();
    
        BaseClass *pb = new ConcreteDecoratorB(pa);
        pb->operation();
    
        delete p;
        delete pa;
        delete pb;
        return 0;
    }

CSharp

BaseClass對象抽象基類:


    public abstract class BaseClass
    {
        public abstract void Operation();
    }

ConcreteClass具體對象實現類:


    public class ConcreteClass : BaseClass
    {
        public override void Operation()
        {
            Console.WriteLine("Concrete Base Operation .");
        }
    }

Decorator裝飾抽象類:


    public abstract class Decorator : BaseClass
    {
        protected BaseClass m_BaseClass;

        public Decorator(BaseClass baseClass)
        {
            m_BaseClass = baseClass;
        }

        public override void Operation()
        {
            if (m_BaseClass != null)
                m_BaseClass.Operation();
        }
    }

ConcreteDecoratorA具體裝飾是實現類:


    public class ConcreteDecoratorA : Decorator
    {
        public ConcreteDecoratorA(BaseClass baseClass) : base(baseClass)
        {

        }

        public void AddBehavior()
        {
            Console.WriteLine("ConcreteDecoratorA 's Decorator Behavior .");
        }

        public override void Operation()
        {
            base.Operation();
            AddBehavior();
        }
    }

客戶端代碼:


    class Program
    {
        static void Main(string[] args)
        {

            ConcreteClass p = new ConcreteClass();

            BaseClass pa = new ConcreteDecoratorA(p);
            pa.Operation();

            BaseClass pb = new ConcreteDecoratorB(pa);
            pb.Operation();

            Console.ReadKey(false);
        }
    }

設計模式——6.裝飾模式