[Design mode] decorator Mode

Source: Internet
Author: User
Problem

During OO design and development, the following situations may occur frequently: we need to add new responsibilities (Operations) for a defined class ), in general, we will define a new class to inherit the custom class, but solving this problem through inheritance brings about the complexity of the system, because the depth of inheritance will become very deep.

The Decorator provides a method to add responsibilities to the class, not through inheritance, but through combination.

Decorator modifier Mode

Dynamically add some additional responsibilities to an object. The extended function is more flexible than the subclass generation method.

Analysis

  • 1) Component: defines an object interface, which can dynamically add roles for this interface
  • 2) Decorator: maintains a pointer to Component and has an interface function consistent with Component.
  • 3) Component: Operation: this interface function is declared by Component. Therefore, all the derived classes of Component must be implemented. You can dynamically add responsibilities to this interface function.

The derived class of Decorator can dynamically add roles to the objects of the ConcreteComponent class, or the derived class of Decorator can deploy the objects of the ConcreteComponent class. Specifically, initialize a ConcreteComponent Class Object (Decorator) and use this object to generate a Decorator object (Decorator ), the subsequent call to the Operation function is a multi-state call to the member function of the Decorator object.

The main point of implementation here is that both the Decorator class and the ConcreteComponent class inherit the Component class, so that the interface functions of the two are the same. Secondly, the Decorator maintains a pointer pointing to the Component, so that the Component can be implemented :: dynamic call of the operation function.

Demo

Decorator. h

# Ifndef DECORATOR_H # define DECORATOR_H // abstract base class, defines an object interface, can dynamically add responsibility for this interface. class Component {public: Component () {} virtual ~ Component () {}// pure virtual function, which implements virtual void Operation () = 0 ;}by the derived class; // abstracts the base class and maintains a pointer class Decorator pointing to the Component object: public Component {public: Decorator (Component * pComponent): m_pComponent (pComponent) {} virtual ~ Decorator (); protected: Component * m_pComponent;}; // derived from Component. class ConcreateComponent: public Component {public: ConcreateComponent () {} virtual ~ ConcreateComponent () {} virtual void Operation () ;}; // derived from Decorator, which indicates class ConcreateDecorator: public Decorator {public: concreateDecorator (Component * pComponent): Decorator (pComponent) {} virtual ~ ConcreateDecorator () {} virtual void Operation (); private: void AddedBehavior () ;}; # endif

 

Decorator. cpp

#include "Decorator.h"#include <iostream>Decorator::~Decorator(){delete m_pComponent;m_pComponent = NULL;}void ConcreateComponent::Operation(){std::cout << "Operation of ConcreateComponent\n";}void ConcreateDecorator::Operation(){m_pComponent->Operation();AddedBehavior();}void ConcreateDecorator::AddedBehavior(){std::cout << "AddedBehavior of ConcreateDecorator\n";}

 

Main. cpp

# Include "Decorator. h "# include <stdlib. h> int main () {// initialize a Component object Component * pComponent = new ConcreateComponent (); // use this Component object to initialize a Decorator object, // You can dynamically Add the role Decorator * pDecorator = new ConcreateDecorator (pComponent); pDecorator-> Operation (); delete pDecorator; system ("pause") for this Component object "); return 0 ;}

 

The Decorator mode and Composite mode have similar structure.

The Decorator mode and Proxy mode are similar in that they all have a reference (pointer) pointing to other objects, that is, they provide more operations (or Decorator mode) for objects through a combination) indirect (Proxy mode ). However, the difference between them is that the Proxy mode provides the same interface as its Proxy object, and uses the Proxy class to delegate all its operations to the Proxy directly. It can be simply understood as the subtle difference between a combination and a delegate.

In addition to the combination method, the Decorator mode achieves better results than the inheritance method. The Decorator mode does not search and brings a "pay-as-you-go" method to the design to add responsibilities. In OO design and development, it is often the case that, for polymorphism, the parent class Pointer Points to its specific subclass, but this brings another problem, to add a new role to a subclass, you must add an abstract interface for this role to its parent class. Otherwise, this method cannot be called through the parent class pointer. In this way, the parent class at the upper level will carry too many features (methods) and inherit all subclasses of the parent class will inevitably inherit these interfaces of the parent class, however, this may not be required by this specific subclass. The Decorator mode provides a better solution. You can use the Decorator mode to add an operation. You can add new roles step by step.

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.