[Study Notes] Decorator in Design Mode

Source: Internet
Author: User

For the convenience of readers, this article has been added to the index: Design Mode learning note index Decorator mode, which can dynamically add some additional functions to an object. To better understand this mode, we pull the timeline back to the end of the Bridge mode note. At that time, Snow White shot an arrow full of magic power. Like a Hollywood blockbuster, The Flying Arrow emits a variety of beautiful rays that you can think. Of course, I won't tell you that Jian Hua is so arrogant. This is because we are more concerned about this splendid magic effect. What I want to say is that when the hacker creates this parallel world, he defines the abstract class VisualObject: 1 class VisualObject {2 public: 3 virtual void show () for all visible objects () = 0; 4 //... other... 5} any object that appears in this world and can be seen by people outside the world (such as US) is its subclass. At the same time, the show () method must be implemented. Show defines the style that this object presents to the audience. Of course, we can see how the magic guide is lazy when creating arrows: 1 class Arrow: public VisualObject {2 public: 3 virtual void show () {cout <"what do you think it is. "<Endl;} 4 //... other... 5} Of course, this is not feasible. It is because some may think of Arrow as a huge shell or even a flying pig to enhance the animation of the story. But in any case, when the magic guide wants to tell everyone that the arrow of the princess is attached to the ice cream effect, he must clearly describe what the arrow looks like. If you are too lazy, you can create an IceArrow sub-class of Arrow. You only need to add more descriptions about ice crystals. But the problem is that, since magic can be attached, arrows can have more and more effects: attach flame, attach lightning, and look better, and so on ...... If each sub-class is used, the number of sub-classes is too large. Besides, we just want to add some special effects to an object instead of the entire class. Also, don't forget to consider anything that can be possessed by arrows. What should I do? The hacker guides use the Decorator mode. Key points: Sorting objective classification Object Schema scope criterion object (this mode processes the relationships between objects, which can be changed and more dynamic at runtime) the main function dynamically adds some additional responsibilities to an object. The new function is more flexible than the subclass generation function. When other objects are not affected, add responsibilities to a single object in a dynamic and transparent manner to process the responsibilities that can be undone when the subclass generation method cannot be used for expansion. For example, there may be a large number of independent extensions. To support each combination, a large number of subclasses will be generated, resulting in explosive growth of the number of subclasses. Part of Component: Define an object interface, you can dynamically Add the ConcreteComponent to these objects: Define an object and add some roles to this object: maintain a pointer to the Component object, define a ConcreteDecorator interface consistent with the Component Interface: Add a functional collaboration process to the Component. Decorator forwards the request to its Component object, it is also possible to execute some additional actions before and after the request forwarding UML diagram Example Analysis-like a magic arrow in a large film, please do not care too much about who the above figure is. To add magic effects to arrows (or even more objects), the magic guide introduces the ObjectDecorator class: Copy code 1 class ObjectDecorator: public VisualObject {2 public: 3 ObjectDecorator (VisualObject *); 4 5 virtual void show (); 6 //... other... 7 private: 8 VisualObject * _ component; 9} 10 11 void ObjectDecorator: show () {12 _ component-> show (); 13} copy the code ObjectDecorator to decorate the VisualObject referenced by the _ component instance variable. This variable is initialized in the constructor. Meanwhile, ObjectDecorator forwards the show request to _ component. MagicDecorator, one of the subclass of ObjectDecorator, will be able to add magic effects to the visualization object: Copy code 1 class MagicDecorator: public ObjectDecorator {2 public: 3 MagicDecorator (VisualObject *, int); 4 virtual void show (); 5 //... other... 6 private: 7 int _ magicType; 8 9 void addMagic (int); 10} 11 12 void MagicDecorator: show () {13 ObjectDecorator: show (); 14 addMagic (_ magicType); 15} copy code similar to, we can also implement other decorative classes. We will put this arrow in this world. Assume that the World class (a singleton class) has provided an add2Me operation for this: 1 void World: add2Me (VisualObject * obj) {2 //... 3 obj-> show (); 4 //... 5} Now we want people outside of the World to see an Arrow. Just do this: 1 arrow * Arrow = new Arrow (); 2 World: getInstance () -> add2Me (arrow. Before placing arrows in the World, we will add a decoration of ice and ice magic to them: 1 # define ICEMAGIC 12 World: getInstance ()-> add2Me (new MagicDecorator (arrow, ICEMAGIC); OK, Snow White emits an arrow with the cold ice magic. The following UML diagram illustrates our example:

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.