《大話設計模式》之 第6章 穿什麼有這麼重要?——裝飾模式 (Decorator 模式) 小結

來源:互聯網
上載者:User

今天看到裝飾者模式,中間提到了建造者模式,雖然還沒看過建造者模式,但是想想看就應該知道二者的區別:

前者是對象的組成是不固定的,比如一個person,想穿幾件clothes都可以,而建造者模式,就是所有的組成都必須是確定的,就好像某個型號的電腦,必須包含主板、顯卡、記憶體……

具體的應用還不太清晰。

但是原理基本上是這樣的:一個【基類Component】,提供統一的介面,子類主要分為【裝飾類DecoratorComponent】和【被裝飾類PersonComponent】

【DecoratorComponent】又可以派生子類【TShirtsDecoratorComponent】、【TrouserDecoratorComponent】……

用以裝飾PersonComponent類。

【基類Component】

//基類class Component{public:    Component(void);    virtual ~Component(void);    virtual void CommonInterface(void) const = 0;};Component::Component(void){    }Component::~Component(void){    }

【被裝飾類PersonComponent】

//被裝飾類class PersonComponent : public Component{public:    PersonComponent(void);    ~PersonComponent(void);    virtual void CommonInterface(void) const;};PersonComponent::PersonComponent(void){    }PersonComponent::~PersonComponent(void){    }void PersonComponent::CommonInterface(void) const{    std::cout << "被裝飾者 小菜\n";}

【裝飾類DecoratorComponent】

//裝飾類class DecoratorComponent : public Component{public:    DecoratorComponent(void);    virtual ~DecoratorComponent(void);    virtual void CommonInterface(void) const;    void setComponent(Component* comp);protected:    Component* component;};DecoratorComponent::DecoratorComponent(void){    }DecoratorComponent::~DecoratorComponent(void){    }void DecoratorComponent::CommonInterface(void) const{    if (this->component != NULL) {        component->CommonInterface();    }}void DecoratorComponent::setComponent(Component* comp){    this->component = comp;}

【裝飾子類TShirtsDecoratorComponent】

//裝飾子類TShirtsDecoratorComponentclass TShirtsDecoratorComponent : public DecoratorComponent{public:    TShirtsDecoratorComponent(void);    virtual ~TShirtsDecoratorComponent(void);    virtual void CommonInterface(void) const;};TShirtsDecoratorComponent::TShirtsDecoratorComponent(void){    }TShirtsDecoratorComponent::~TShirtsDecoratorComponent(void){    }void TShirtsDecoratorComponent::CommonInterface(void) const{    DecoratorComponent::CommonInterface();    std::cout << "TShirts Decorated!\n";}

【裝飾子類TrouserDecoratorComponent】

//裝飾子類TrouserDecoratorComponentclass TrouserDecoratorComponent : public DecoratorComponent{public:    TrouserDecoratorComponent(void);    virtual ~TrouserDecoratorComponent(void);    virtual void CommonInterface(void) const;};TrouserDecoratorComponent::TrouserDecoratorComponent(void){    }TrouserDecoratorComponent::~TrouserDecoratorComponent(void){    }void TrouserDecoratorComponent::CommonInterface(void) const{    DecoratorComponent::CommonInterface();    std::cout << "Trouser Decorated!\n";}

【main】

int main(int argc, const char * argv[]){    PersonComponent* person = new PersonComponent();    TShirtsDecoratorComponent* tShirts = new TShirtsDecoratorComponent();    TrouserDecoratorComponent* trouser = new TrouserDecoratorComponent();    tShirts->setComponent(person);    trouser->setComponent(tShirts);    trouser->CommonInterface();        delete trouser;    trouser = NULL;    delete tShirts;    tShirts = NULL;    delete person;    person = NULL;        return 0;}

【結果】

被裝飾者 小菜

TShirts Decorated!

Trouser Decorated!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.