Decorator Pattern

//-----DECORATOR PATTERN------//

MainFile.cs

using System;

namespace Pattern_Decorator
{
    class Program
    {
        static void Main(string[] args)
        {
            Beverage beverage1 = new Espresso();
            Console.WriteLine("Beverage: {0}, Cost: {1}",beverage1.Description,beverage1.Cost());

            Beverage beverage2 = new DarkRoast();
            beverage2 = new Mocha(beverage2);
            beverage2 = new Mocha(beverage2);
            beverage2 = new Whip(beverage2);
            Console.WriteLine("Beverage: {0}, Cost: {1}", beverage2.Description, beverage2.Cost());

            Beverage beverage3 = new HouseBlend();
            beverage3 = new Soy(beverage3);
            beverage3 = new Mocha(beverage3);
            beverage3 = new Whip(beverage3);
            Console.WriteLine("Beverage: {0}, Cost: {1}", beverage3.Description, beverage3.Cost());

        }
    }
}


DecoratorClass.cs

using System;

namespace Pattern_Decorator
{
    public abstract class Beverage
    {
        string description = "Unknown Beverage";
        public abstract double Cost();
        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }
    }

    public abstract class CondimentDecorator : Beverage
    {
    }

    #region Beverages

    public class Espresso : Beverage
    {

        public Espresso()
        {
            Description = "Espresso";
        }
        public override double Cost()
        {
            return 1.99;
        }
    }

    public class HouseBlend : Beverage
    {

        public HouseBlend()
        {
            Description = "HouseBlend";
        }
        public override double Cost()
        {
            return 0.89;
        }
    }

    public class DarkRoast : Beverage
    {

        public DarkRoast()
        {
            Description = "DarkRoast";
        }
        public override double Cost()
        {
            return 2.50;
        }
    }

    public class Decaf : Beverage
    {

        public Decaf()
        {
            Description = "Decaf";
        }
        public override double Cost()
        {
            return 2.00;
        }
    }

    #endregion

    #region Condiments

    public class Mocha : CondimentDecorator
    {
        Beverage beverage;
        public Mocha(Beverage beverage)
        {
            this.beverage = beverage;
            Description = this.beverage.Description + ", Mocha";
        }
        public override double Cost()
        {
            return this.beverage.Cost() + 0.50;
        }
    }

    public class Soy : CondimentDecorator
    {
        Beverage beverage;
        public Soy(Beverage beverage)
        {
            this.beverage = beverage;
            Description = this.beverage.Description + ", Soy";
        }
        public override double Cost()
        {
            return this.beverage.Cost() + 1.50;
        }
    }

    public class Whip : CondimentDecorator
    {
        Beverage beverage;
        public Whip(Beverage beverage)
        {
            this.beverage = beverage;
            Description = this.beverage.Description + ", Whip";
        }
        public override double Cost()
        {
            return this.beverage.Cost() + 0.10;
        }
    }

    #endregion

}
Ebook Download
View all
Learn
View all