Design Mode (9) DECORATOR Mode)

Source: Internet
Author: User

Problem focus: Sometimes, when you want a class to add new functions, a subclass is usually generated. However, this method is sometimes a little bloated. Secondly, this method is static and not flexible enough. Therefore, in response to this demand for dynamic addition of functions to objects, Daniel provides the decorator mode in this section.
Intention: dynamically add some additional responsibilities to the next account. With the added function, the decorator mode is more flexible than the subclass generation.
Alias: wrapper
Motivation: add some functions to an object instead of the entire class. For example, a graphical user interface toolbox. Requirement: allows you to add some features, such as borders, or some actions, such as window scrolling, to any user interface. Inheritance Mechanism: using the Inheritance Mechanism is an effective way to add functions. The border feature inherited from other classes can be used by instances of multiple subclasses. Disadvantages of the Inheritance Mechanism: not flexible enough, because the selection of borders is static, users cannot control the way and time to add borders to components. Decorator: embed a widget into another object, and add a border to the object. We call this embedded object decoration. The decoration method features: This decoration is consistent with the component interface it is decorated with, so it is transparent to customers who use this component. The advantage of the decoration method: transparency. It forwards customer requests to this component and may perform some additional actions before and after forwarding. At the same time, you can recursively nest Multiple decorations to add any number of functions. Demo: Assume that there is an object TextView, which can display the body in the window. Requirement: When necessary, add a scroll bar (use ScrollDecorator) or add a thick black border (use BorderDecorator ). Relationship between classes: the decoration process is shown in: The Paster class graph design of the visual object: The abstract class that the visual component describes the visual object, and defines the interface for drawing and event processing. The subclass of Decorator allows you to add some operations for specific functions. Advantage: Customers generally do not feel the difference between decorated components and undecorated components, nor have any dependency with decoration. Because the decorator is used by other objects on the interface. It is not directly used by the customer.
Applicability:

  • The decorator mode is used as follows:
  • Add roles to a single object dynamically and transparently without affecting other objects
  • Handle revoking responsibilities
  • When the subclass generation method cannot be used for expansion. Subclass cannot be used in two cases: a large number of independent extensions may exist. To support each extension, the number of subclasses may increase explosively. In another case, the class definition is hidden, or the class definition cannot be used to generate a subclass. Structure:
    Participants:
    • Component (VisualComponent): defines an object interface to dynamically add roles to these objects.
    • ConcreteComponent (TextView): defines an object. You can add some responsibilities to this object.
    • DecotatZ environment? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys/keys + keys/y1 + m8/sztvNPWsNTwCtCt1/keys/PXqreiuPjL/keys/ycTc1NrX6reix + keys/ehowo8YnI + examples/MHpu + examples/ybao1sa1xMDgyc/Wp7PWo6y2 + MrHx + examples/samples + vdO/examples/ztcS907/asdjQ69Pry/zL + examples/ release + yqHC1LPpz/release/O5/release + zc/release + release/JxNzKx9K7uPa4/release + release/aOsy7XD98jnus7Ktc/resume "brush: java; "> // assume that a Component class VisualComponentclass VisualComponent {public: VisualComponent (); virtual void Draw (); virtual void Resize (); //......}; // We define a sub-class Decorator of VisualComponent, which will generate a sub-class of Decorator to obtain different decorative class Decorator: public VisualComponent {public: Decorator (VisualComponent *); virtual void Draw (); virtual void Resize ();//...... private: VisualComponent * _ component; // This example variable is initialized in the constructor and forwarded through this variable}; // for each operation defined in the VisualComponent interface, the Decorator class defines a default implementation, which forwards the request to _ componentvoid Decorator: Draw () {_ component-> Draw ();} void Decorator :: resize () {_ component-> Resize () ;}// The subclass of Decorator defines the special decoration function class BorderDecorator: public Decorator {public: BorderDecorator (VisualComponent *, int borderWidth); virtual void Draw (); private: void DrawBorder (int); private: int _ width ;}; void BorderDecorator: Draw () {Decorator :: draw (); DrawBorder (_ width) ;}// similar functions can be used to implement ScrollDecorator and DropShadowDecorator, and add the scroll and shadow functions to the interface.
      The following code shows how to use the decorator to create a scrolling TextView with boundaries.
      // First, put a visible component into the Window object. This operation is the SetContents operation void Window: SetContents (VisualComponent * contents) in the Window ){//......} // create a text view and put it in Window * window = new Window; TextView * textView = new TextView; window-> SetContents (textView); // if you want to add a scroll bar, we need to decorate window-> SetContents (new BorderDecorator (new ScrollDecorator (textView), 1) before placing it in the window ));

      Related mode: the adapter mode gives the object a set of brand-new interfaces, while the decoration mode does not change the interface of the object. It only changes the combination mode of duties of the object: The decoration mode can be considered as a degradation, A combination of only one component. Strategy Mode: You can use a decorator to change the appearance of an object, while Strategy mode allows you to change the kernel of an object. In this case, two ways to dynamically change objects are provided.

      Reference: Design Pattern: Basis for reusable Object-Oriented Software

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.