Decorator decoration Mode

Source: Internet
Author: User

The decorator decoration mode is a structural mode. It mainly solves the problem of excessive use of inheritance to extend object functions. The decorator mode is more flexible than the subclass generation function.

The decorator decoration mode is a structural mode, which mainly solves the following problems: "excessive use of inheritance to extend object functions", because inheritance is a type-introduced static characteristic, the lack of flexibility in this expansion method. As the number of child classes increases (the number of extended functions), the combination of various child classes (the combination of extended functions) it will lead to expansion of more sub-classes (more inheritance ). Inheritance refers to the static characteristics introduced by the type, which means that the function to be obtained for a certain type in the inheritance mode is compiled. Static refers to compiling; Dynamic refers to running. Gof "design patterns" says: dynamically add some additional responsibilities to an object. The decorator mode is more flexible than the subclass generation function. Let's take a look at the structure of the decorator mode: it seems that this structure is not quite clear. I will explain this structure according to the code below. I thought about a scenario: we now use a lot of mobile phone functions, and I will use the decorator mode to implement GSP and Bluetooth function extensions for a mobile phone. First, we need a mobile phone interface or abstract class. Here we use abstract classes for implementation. The Code is as follows: public abstract class implements actcellphone {public abstract string callnumber (); public abstract string sendmessage ();} abstractcellphone is the component in the structure diagram. Then, I will implement the mobile phone classes of Nokia and Moto. This class must inherit the abstractcellphone class, that is, the concretecomponent class in the diagram must inherit the component class, the implementation code is as follows: public class nokiaphone: abstractcellphone {public override string callnumber () {return "nokiaphone call sombody";} Pu BLIC override string sendmessage () {return "nokiaphone send a message to somebody" ;}} public class motophone: abstractcellphone {public override string callnumber () {return "motophone call sombody ";} public override string sendmessage () {return "motophone send a message to somebody" ;}} next I need a decorator interface or abstract class. The implementation code is as follows: public abstract class decorator: abstractcellphone {abstractcellphone _ Phone; Public decorator (abstractcellphone phone) {_ phone = phone;} public override string callnumber () {return _ phone. callnumber ();} public override string sendmessage () {return _ phone. sendmessage () ;}} as in the structure diagram, this decorator inherits abstractcellphone and contains a private abstractcellphone object. This means that the decorator class uses another component class. We can use one or more decorator objects to "Decorate" a component object, and the decorated object is still a component object. Next, I want to implement GSP and Bluetooth function extensions. They must inherit from the decorator. The Code is as follows: public class decoratorgps: decorator {public decoratorgps (abstractcellphone phone): Base (phone) {} public override string callnumber () {return base. callnumber () + "with GPS";} public override string sendmessage () {return base. sendmessage () + "with GPS" ;}} public class decoratorbluetooth: decorator {public decoratorbluetooth (abstractcellphone Pho Ne): Base (phone) {} public override string callnumber () {return base. callnumber () + "with Bluetooth";} public override string sendmessage () {return base. sendmessage () + "with Bluetooth" ;}} Finally, use the client program to verify: static void main (string [] ARGs) {abstractcellphone = new nokiaphone (); console. writeline (phone. callnumber (); console. writeline (phone. sendmessage (); decoratorgps GPS = new decorator GPS (phone); // Add GSP console. writeline (GPS. callnumber (); console. writeline (GPS. sendmessage (); decoratorbluetooth Bluetooth = new decoratorbluetooth (GPS); // Add GSP and Bluetooth console. writeline (Bluetooth. callnumber (); console. writeline (Bluetooth. sendmessage (); console. read ();} execution result: nokiaphone call sombodynokiaphone send a message to somebodynokiaphone call sombody with gpsnokiaphone send Message to somebody with gpsnokiaphone call sombody with GPS with descrithnokiaphone send a message to somebody with GPS with Bluetooth it is not difficult to see that the extension function has been added from the execution results. Finally, let's talk about the key points of the decorator decoration mode: 1. By using combinations rather than inheritance, the decorator mode can dynamically expand object functions at runtime, you can also expand multiple functions as needed. This avoids the "poor flexibility" and "Multi-subclass Derivative Problems" caused by the independent use of inheritance ". 2. The component class acts as an abstract interface in the decorator mode and should not implement specific actions. In addition, the decorator class should be transparent to the component class. In other words, the component class does not need to know the decorator class. The decorator class extends the component class functions from the outside. 3. the decorator class represents the inheritance relationship of is-a component on the interface, that is, the decorator class inherits the interface of the component class. However, the implementation is manifested in the composite relationship of has-a component, that is, the decorator class uses another component class. We can use one or more decorator objects to "Decorate" a component object, and the decorated object is still a component object. (Here I want to talk about my understanding: After we instantiate a component object, we need to extend the function of this object, in this case, the component object is passed as a parameter to the constructor of the decorator subclass-that is, the function class of the extension method. When passing parameters of the reference type, it actually only transmits the object address. In this way, the function extension is that the operation should be the same object) 4. the decorator mode does not solve the problem of "Multi-subclass derived multi-inheritance, the main point of the decorator mode application is to solve the problem of "the extension function of the Subject Class in multiple directions", which is the meaning of "decoration. Decorator is a combination of functions at runtime.

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.