Decorator pattern)

Source: Internet
Author: User

Assume that a company wants to make a product package, that is, to combine different products, and different combinations correspond to different prices. The final result is that all elements of the product combination are displayed and the price of the combination is displayed.

 

Each product has a name and a price. First, design an abstract base class for the product.

    public abstract class ProductBase
    {
        public abstract string GetName();
        public abstract double GetPrice();
    }

 

All products must inherit this base class, such as household products and electrical products, and extract these specific products into a subclass that inherits productbase.

 

    public class ConcretProuct : ProductBase
    {
        private string _name;
        private double _price;
        public ConcretProuct(string name, double price)
        {
            this._name = name;
            this._price = price;
        }
        public override string GetName()
        {
            return _name;
        }
        public override double GetPrice()
        {
            return _price;
        }
    }

 

Then consider the product portfolio. For example, if you want to sell a pan, soy sauce may be sent, or soy sauce + altar sour dishes may be sent. The possible combinations include:
○ Pan
○ Pan + soy sauce
○ Pan + soy sauce + old altar sauerkraut

 

Here, we can regard soy sauce and old altar pickled cabbage as a decoration device, because each added product is added on the basis of the original. For example, the "pan + soy sauce" Combination adds "Soy Sauce" on the basis of "pan ".

 

Now we have designed soy sauce and laotan pickled cabbage as subclasses inherited from productbase, that is, the ornament class. However, unlike the concretprouct class, the decoration class must reference productbase. Here, the referenced productbase is indispensable for both displaying the product portfolio and calculating the product portfolio price.

 

   public class Decorator : ProductBase
    {
        private ProductBase _product = null;
        private string _name;
        private double _price;
        public Decorator(ProductBase product, string name, double price)
        {
            this._product = product;
            this._name = name;
            this._price = price;
        }
        public override string GetName()
        {
            return string.Format("{0},{1}", _product.GetName(), _name);
        }
        public override double GetPrice()
        {
            return _product.GetPrice() + _price;
        }
    }

 

When the product name is displayed, the name of the productbase referenced by the decorator class decorator is combined with the current name and separated by commas. When the product price is displayed, add the price of the referenced productbase to the current price.

 

The client is as follows:

    class Program
    {
        static void Main(string[] args)
        {
Concretprouct livingproduct = new concretprouct ("pan", 100 );
            Console.WriteLine(PrintProductDetails(livingProduct));
Decorator dec1 = new decorator (livingproduct, "seafood soy sauce", 10 );
            Console.WriteLine(PrintProductDetails(dec1));
Decorator dec2 = new decorator (dec1, "laotan pickles", 12 );
            Console.WriteLine(PrintProductDetails(dec2));
            Console.ReadKey();
        }
        private static string PrintProductDetails(ProductBase product)
        {
Return string. Format ("product portfolio: {0} price: {1}", product. getname (), product. getprice ());
        }
    } 


Decorator pattern)

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.