Decorator of design patterns

Source: Internet
Author: User

Decorator of design patterns
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 = 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: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPjxicj4KPHA + partition = "http://www.2cto.com/uploadfile/Collfiles/20140811/2014081110212398.png" alt = "" http://www.bkjia.com/ OS/"target =" _ blank "class =" keylink "> many small objects are generated during system design.
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.









Related Article

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.