C #-Design pattern-Decorator mode

Source: Internet
Author: User

Decorator ModeProblem Scenario

If you want to calculate the price of a cup of coffee, you only need to call the method to get the price, but if you need to add some materials, such as milk, chocolate, sugar and so on, these materials must return their prices to facilitate the summary calculation, but there are many materials, we can not predict beforehand what the buyer needs materials, So it seems that only in the coffee class to make a decision to confirm the buyer needs the material to calculate the results, but this is not a good solution, because the material may have 100 kinds, we can not write 100 times to determine which materials are added, and in the future there are new materials, but also to continue to modify the code in many places. The solution is to use abstract classes and wrappers, abstract classes provide overridable methods of obtaining prices, the beverage subclass to achieve its own price, and the material can be derived from the same abstract class, so that the beverage class only need to return its own price, and the material class is dependent on the implementation of the abstract class (beverage Class), Invoking the abstract class's method of acquiring the price and adding the price of the material itself can be used to calculate the total price when the material is externally invoked.

Summary Mode

Without altering the type structure, only the existing code is used to attach additional functionality to the type. The decorator is also called the wrapper, and the wrapper needs to rely on an abstract class's concrete implementation class in order to invoke the method of the dependent object to get the result.

code Example namespace appcui
{
//Super class
Public abstract class Drink
    {
Public abstract double getprice( );
    }

//Specific drinks
Public class coffe : Drink
    {
//Return to their own price
Public override double getprice( )
        {
return 12.5;
        }
    }

//Specific materials are also derived from beverages in order to rewrite the price
Public class Milk : Drink
    {
//The price of the drink needs to be attached to my price, so I need to get a drink in order to calculate the total price
Private Drink Drink;

//Beverage abstract specific class injected into me through the constructor function
Public Milk( Drink Drink )
        {
This . Drink = Drink ;
        }

//Calculate Total Price
Public override double getprice( )
        {
return 1.2 + drink. GetPrice ( );
        }
    }

Public class chocolate : Drink
    {
Private Drink Drink;

Public chocolate( Drink Drink )
        {
This . Drink = Drink ;
        }

Public override double getprice( )
        {
return 1.5 + drink. GetPrice ( );
        }
    }

Public class programe
    {
static void Main( string[] args ) /c14>
        {
Drink Drink = new coffe( ); //I'd like a cup of coffee .
drink = new Milk( drink ); //Coffee needs milk
drink = new chocolate( drink ); //Milk coffee needs sugar
Console. WriteLine ( drink. GetPrice ( ) ); //Chocolate inside maintains the price of milk coffee, milk maintains the price of coffee, return path: coffee Price = milk + Coffee price + chocolate + Milk coffee price
        }
    }
}

  

C #-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.