Decorator of design patterns

Source: Internet
Author: User
Deocrator)
By dynamically adding some additional responsibilities to an object, the decoration mode is more flexible than the subclass generation.




The so-called Decoration refers to some objects to accompany the theme objects. As we can imagine, in a company, everyone has a job that requires computers, telephones, folders, potted plants, pen signatures, and seals as decoration. However, different people may have different decorations on their desks. For example, a boss should have everything on his desk, but a general employee's desk should not have a phone number or an official seal. How can we dynamically decorate our desks?

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace decoration {class program {// desk public class officedesk {public officedesk () {} private string username; Public officedesk (string username) {This. username = username;} Public Virtual void show () {console. writeline ("{0} desk:", username) ;}/// decorator public class decorator: officedes K {// You Need To Know the decoration object, which corresponds to the aggregate protected officedesk desk in UML; // dress up the desk public void setdecortate (officedesk desk) {This. desk = desk;} public override void show () {If (desk! = NULL) {desk. show () ;}}// specific decoration class // computer public class computer: decorator {public override void show () {base. show (); console. writeline ("A computer") ;}}// call public class Telephone: decorator {public override void show () {base. show (); console. writeline ("A phone number") ;}// folder public class file: decorator {public override void show () {base. show (); console. writeline ("a folder"); }}// potted public class plant: decorator {public override void show () {base. show (); console. writeline ("pot planting") ;}// signature pen public class pen: decorator {public override void show () {base. show (); console. writeline ("A pen") ;}// public class seal: decorator {public override void show () {base. show (); console. writeline ("an official seal") ;}} static void main (string [] ARGs) {officedesk renzong = new officedesk ("Ren Zong"); computer = new computer (); telephone telephone = new telephone (); file = new file (); plant = new plant (); Pen = new pen (); seal = new seal (); computer. setdecortate (renzong); telephone. setdecortate (Computer); file. setdecortate (telephone); plant. setdecortate (File); pen. setdecortate (plant); seal. setdecortate (PEN); seal. show (); console. writeline (); officedesk Xiaoli = new officedesk ("Xiao Li"); computer. setdecortate (Xiaoli); file. setdecortate (Computer); pen. setdecortate (File); pen. show (); console. read ();}}}





Decoration class diagram:




The decoration mode structure includes the following roles:
Component (Abstract component): It is the common parent class of a specific component and abstract decoration class, and declares the business methods implemented in a specific component, its introduction allows the client to process undecorated objects and decorated objects in a consistent manner to implement transparent operations on the client.
Concretecomponent (specific component): It is a subclass of the abstract component class. It is used to define a specific component object and implement the methods declared in the abstract component. The modifier can add additional responsibilities (methods) to it ).
Decorator (Abstract decoration class): It is also a subclass of abstract component classes. It is used to add responsibilities to specific components, but the specific responsibilities are implemented in its subclass. It maintains a reference pointing to an abstract component object. Through this reference, you can call the method of the component object before decoration, and extend this method through its subclass to achieve the purpose of decoration.
Concretedecorator (Specific decoration class): It is a subclass of abstract decoration classes and is responsible for adding new responsibilities to components. Each specific decoration class defines some new behaviors. It can call the methods defined in the abstract decoration class and add new methods to expand the behavior of objects.




Main advantages:
1. For the function of extending an object, the decoration mode is more flexible than inheritance, and does not cause a sharp increase in the number of classes.
2. You can use a dynamic method to expand the functions of an object. You can use the configuration file to select different decoration classes at runtime to implement different behaviors.
3. You can decorate an object multiple times. By using different decorative classes and the arrangement and combination of these decorative classes, you can create a combination of many different behaviors to obtain more powerful objects.
4. The specific component classes and specific decoration classes can be changed independently. You can add new specific component classes and specific decoration classes as needed. The original class library code does not need to be changed and complies with the "open and closed principle ".


Main disadvantages:
1. Many small objects will be generated during system design using the decoration mode.
2. the decoration mode provides a more flexible and mobile solution than inheritance, but it also means more error-prone and difficult troubleshooting than inheritance. For objects decorated multiple times, you may need to troubleshoot errors during debugging.


Applicable scenarios:
1. When we need to dynamically Add a new function or role to an existing object, we can consider using the modifier.
2. The decoration mode can be used when the system cannot be extended by inheritance or if inheritance is not conducive to system expansion and maintenance.


Differences from adapters and policies:

The modifier dynamically adds new behaviors without changing the original content;

The adapter is mainly used to fill in the differences between two interfaces and convert one interface to another;

The policy is to change the function by switching the algorithm.









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.