Design Pattern: Decorator mode

Source: Internet
Author: User

  Design Pattern Series Catalog

Decorator mode, in life we are often in contact. For example, like our fast-paced life, a lot of the morning to buy pancakes. In general we will say: "A coarse-grain pancake, add two eggs , add a sausage "

Or: "To a Shandong Pancake, only add potato silk " and so on. "Pancake" is the elastic object, the dough is unchanged, and other like eggs, sausage and other people in the decoration of bread. This is also a design principle when we program.

Open for extensions, close for modifications. (In the end I'll give C # and C + + two code examples)

Decorator mode is similar to "pancake", it can dynamically take responsibility ("egg", "sausage" ...) Attached to the object ("pancake"). To extend functionality, decorators provide a more resilient alternative than inheritance.

Let's look at the class diagram of the decorator pattern:

  

Looking at this class diagram, we can summarize the characteristics of the decorator pattern:

1. The decoration objects and decorations have the same interface. So decorations and objects can interact with each other.

2. The Ornament object contains a reference to a decorated object.

Let's make a pancake:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacedecoratordemo{[Flags] Public enumBctype {None=0,//0Sdcaketype,//Shandong PancakeEggtype,//EggSausagetype//Sausage    }     Public InterfaceIbattercake {List<BcType>Type (); DoubleCoast (); }     Public classNonebattercake:ibattercake { PublicList<bctype>Type () {return NewList<bctype>{Bctype.none}; }         Public DoubleCoast () {return 0; }    }    /// <summary>    ///Shandong Pancake/// </summary>     Public classSdbattercake:ibattercake { PublicList<bctype>Type () {return NewList<bctype>{Bctype.sdcaketype}; }         Public DoubleCoast () {return 3.5; }    }    /// <summary>    ///Egg/// </summary>     Public classEgg:ibattercake {Private ReadOnlyIbattercake _battercake =NewNonebattercake ();  PublicEgg (Ibattercake mmoney) {_battercake=Mmoney; }         PublicList<bctype>Type () {List<BcType> types =_battercake.type (); Types. AddRange (NewList<bctype>() {bctype.eggtype}); returntypes; }         Public DoubleCoast () {return 1+_battercake.coast (); }    }    /// <summary>    ///Sausage/// </summary>     Public classSausage:ibattercake {Private ReadOnlyIbattercake _battercake =NewNonebattercake ();  PublicSausage (ibattercake mmoney) {_battercake=Mmoney; }         PublicList<bctype>Type () {List<BcType> types =_battercake.type (); Types. AddRange (NewList<bctype>() {bctype.sausagetype}); returntypes; }         Public DoubleCoast () {return 2+_battercake.coast (); }    }    /// <summary>    ///Calculate cost/// </summary>     Public classCalculatemoney:ibattercake {Private ReadOnlyIbattercake _battercake =NewNonebattercake ();  PublicCalculatemoney (Ibattercake mmoney) {_battercake=Mmoney; }         PublicList<bctype>Type () {return NewList<bctype>{Bctype.none}; }         Public stringDescription () {List<BcType> types =_battercake.type (); intEggs =0, sausage =0, battercakies =0;  for(intindex =0, Count = types. Count (); Index < count; index++)            {                if(Types[index] = =bctype.sdcaketype) battercakies++; if(Types[index] = =bctype.eggtype) Eggs++; if(Types[index] = =bctype.sausagetype) Sausage++; } StringBuilder Description=NewStringBuilder (); if(Battercakies >0) description. Append ("Pancakes"). Append (battercakies). Append ("a"). Append ("\ n"); if(Eggs >0) description. Append ("Egg"). Append (eggs). Append ("a"). Append ("\ n"); if(Sausage >0) description. Append ("Grilled Sausage"). Append (sausage). Append ("a"). Append ("\ n"); returndescription.        ToString (); }         Public DoubleCoast () {return_battercake.coast (); }    }     Public classProgram {Private Static voidMain (string[] args) {            //a Shandong PancakeIbattercake Sdbattercake =NewSdbattercake (); //an eggIbattercake EGG1 =NewEgg (Sdbattercake); //one more egg.Ibattercake EGG2 =NewEgg (EGG1); //one more sausage.Ibattercake sausage =NewSausage (EGG2); Calculatemoney Calculatemoney=NewCalculatemoney (sausage);            Console.WriteLine (Calculatemoney.description ()); Console.WriteLine ("Total cost: {0}", Calculatemoney.coast ());        Console.ReadLine (); }    }}

Look at the results:

The following conditions use Decorator Mode 1. You need to extend the functionality of a class or add additional responsibilities to a class. 2. The need to dynamically add functionality to an object, these functions can be re-dynamic revocation. 3. It is necessary to increase the number of functions resulting from the permutations of some basic functions, thus making the inheritance relationship impractical. 4. When a method of generating subclasses cannot be used for expansion. One scenario is that there may be a large number of independent extensions that will produce a large number of subclasses to support each combination, resulting in an explosive increase in the number of subclasses. Another situation may be because the class definition is hidden, or the class definition cannot be used to generate subclasses.

Design Pattern: Decorator mode

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.