Decorator (decoration)-Object-structured mode

Source: Internet
Author: User

1. Intentions

Add some extra responsibilities to an object dynamically. For added functionality, the decorator mode is more flexible than generating subclasses.

2. Aliases

Wrapper Wrapper.

3. Motivation

Add some functionality to an object rather than to the entire class. A more flexible way to embed a component in another object.

4. Applicability

    • Add responsibilities to a single object in a dynamic, transparent manner without affecting other objects.
    • Handle the duties that can be revoked.
    • When the method of subclasses cannot be expanded. One scenario is that there may be a large number of independent extensions that will produce a large number of subclasses to support each combination, resulting in an explosive increase in the number of subclasses. Another situation may be because the class definition is hidden or the class definition cannot be used to generate subclasses.

5. Structure

6. Code Examples

#include <memory>classcomponent{ Public:    Virtual voidOperation () =0;};classConcretecomponent: Publiccomponent{ Public:    voidoperation ();};classDecorator: Publiccomponent{ Public:    Virtual voidoperation (); voidSetComponent (std::shared_ptr<component>pcomponent);protected: Std::shared_ptr<Component>m_pcomponent;};classConcretedecoratora: Publicdecorator{ Public:     voidoperation (); voidOtherbehavior ();};classConcretedecoratorb: Publicdecorator{ Public:     voidoperation (); voidOtherbehavior ();};
#include"Component.h"#include<iostream>voidconcretecomponent::operation () {std::cout<<"concretecomponent Operation executed"<<Std::endl;}voiddecorator::operation () {m_pcomponent-operation ();}voidDecorator::setcomponent (std::shared_ptr<component>pcomponent) {m_pcomponent=pcomponent;}voidconcretedecoratora::operation () {decorator::operation (); Otherbehavior ();}voidConcretedecoratora::otherbehavior () {std::cout<<"Concetedecoratora Otherbehavior executed"<<Std::endl;}voidconcretedecoratorb::operation () {decorator::operation (); Otherbehavior ();}voidConcretedecoratorb::otherbehavior () {std::cout<<"Concretedecoratorb Otherbehavior executed"<<Std::endl;}
#include"Component.h"intMain () {std::shared_ptr<Component> Pcomponent (Newconcretecomponent); Std::shared_ptr<Decorator> Pdecoratora (NewConcretedecoratora); Pdecoratora-setcomponent (pcomponent); Pdecoratora-operation (); Std::shared_ptr<Decorator> Pdecoratorb (NewConcretedecoratorb); Pdecoratorb-setcomponent (pcomponent); Pdecoratorb-operation ();  while(1);}

7. Test results

8. Effects

    • More flexible than static inheritance can add and detach methods, adding and removing duties at run time with decorations.
    • Avoid having too many features in classes at the top level of the hierarchy
    • Decorator is not the same as its component decorator is a transparent wrapper. If we start with an object representation, a decorated component is different from this one, so the use of decorations should not depend on the object identity.
    • There are many small objects that look like small objects that differ only in the way they are interconnected, not their classes or their property values. For those who understand these systems, it's easy to customize them, but it's hard to learn them.

Decorator (decoration)-Object-structured mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.