Definition: dynamically attaches responsibility to the object. To extend functionality, adorners provide a more resilient alternative than inheritance.
Class Diagram:
Component: Define a docking interface that can dynamically add responsibilities to these objects;
concretecomponent: Define a specific object, or you can add some responsibilities to the object;
Decorator: Decorate abstract class, inherit the component, extend the function of component class from outer class, for component need not know the existence of Decorator;
Concretedecorator: The concrete adornment object, plays the function which adds the responsibility to the component;
The decorator and the decorator must be the same type, that is, to have a common superclass. The application of inheritance here is not a copy of the implementation method, but rather a match of the type. Because the decorator and the decorator are of the same type, the decorator can replace the decorator so that the decorator has the unique behavior of the decorator. Based on the concept of decorator mode, we can add new behaviors to new decorators at any time. If you are using inheritance, you should modify the original program whenever you need to add new behavior.
Example: in the display of news at the same time to read out the popularity of the news plus one, the news headlines added to the RSS, if the use of inheritance, it is necessary to add the corresponding method in the superclass, if it is a new behavior can be tolerated, if this module has the possibility of increasing business, Do you want to change the original program every time? I don't think all of my friends want to do it. We want to add to the old object when there is a new behavior, and add it at runtime, not at the beginning.
//#ifdef Component_new_h//#define COMPONENT_NEW_H#include<iostream>#include<string>using namespacestd;classcomponent_new{ Public: Virtual voidGetnew () =0;};classDb_new: Publiccomponent_new{ Public: voidgetnew () {cout<<"getting data from a database"<<Endl; }};classXml_new: Publiccomponent_new{ Public: voidgetnew () {cout<<"getting data from XML"<<Endl; }};classDecorator_new: Publiccomponent_new{ Public: decorator_new (component_new&componentnew) {_component_new= &componentnew; } voidgetnew () {_component_new-getnew (); }Private: Component_new*_component_new;};classConcretedecorator_addamount: Publicdecorator_new{ Public: Concretedecorator_addamount (component_new&componentnew):D ecorator_new::D ecorator_new (componentnew) {//decorator_new::D ecorator_new (&componentnew); } voidAddamount () {cout<<"News popularity has added a"<<Endl; } voidgetnew () {addamount (); Decorator_new::getnew (); }};classConcretedecorator_addrss: Publicdecorator_new{ Public: Concretedecorator_addrss (component_new&componentnew):D ecorator_new::D ecorator_new (componentnew) {}voidAddrss () {cout<<"News headlines have been added to RSS"<<Endl; } voidgetnew () {Addrss (); Decorator_new::getnew (); }};
#include <iostream>"component_news.h"int main () { Component_newNew db_new (); Decorator_newNew concretedecorator_addamount (*_component_new); New Concretedecorator_addrss (*_decorator_new); _decorator_new, Getnew (); return 0 ;}
Advantages:
1) Remove the decoration function from the class to simplify the original class;
2) effectively separates the core functional areas of the class and removes the repetitive decoration logic in the class;
Design pattern Implementation C + +-Decorative mode decorator pattern