Decorator Pattern


As the name implies Decorator Pattern is about assigning additional responsibilities to an existing type during run time.

“Decorating an object during runtime” In short.

Before I talk about interception using unity I felt it is worth writing about decorator pattern to set the right context.

I will try explaining the decorator pattern while building a component which encapsulates this pattern:

Lets say there is a concrete base class called Pizza

 

public abstract class Pizza
    {
        public double price;
        public virtual double GetPrice()
        {
            return this.price;
        }
    }

Since this is the base class, let’s make two main flavors of it, Barbeque and Gourmet

 

public class Barbeque : Pizza
    {
        public Barbeque()
        {
            this.price = 2.0;
        }
    }

    public class Gourmet : Pizza
    {
        public Gourmet()
        {
            this.price = 4.0;
        }
    }

I have created two flavors, Barbeque price starts $2 and Gourmet starts $4.Now Lets create few toppings…

Now toppings are our decorators, so we need to create a concrete decorator base class from which different types of decorators will inherit.

 

    public class ToppingsDecorator : Pizza
    {
        private Pizza pizza;

        public ToppingsDecorator(Pizza pizzaToDecorate)
        {
            this.pizza = pizzaToDecorate;
        }

        public override double GetPrice()
        {
            return this.pizza.GetPrice() + this.price;
        }
    }

From this decorator we are going to inherit two types…cheese decorator and Jalapeno decorator

 

    public class CheeseDecorator : ToppingsDecorator
    {
        public CheeseDecorator(Pizza pizzaToDecorate)
            : base(pizzaToDecorate)
        {
            this.price = 3.0;
        }
    }

    public class JalapenoDecorator : ToppingsDecorator
    {
        public JalapenoDecorator(Pizza pizzaToDecorate)
            : base(pizzaToDecorate)
        {
            this.price = 5.0;
        }
    }

Now that we have all ingredients ready, lets see how it makes a nice recipe…

 

            Pizza myPizza = new Gourmet();

            CheeseDecorator cheeseDecortor = new CheeseDecorator(myPizza);
            CheeseDecorator extraCheeseDecortor = new CheeseDecorator(cheeseDecortor);

            JalapenoDecorator jalapenoDecorator = new JalapenoDecorator(extraCheeseDecortor);
            JalapenoDecorator extrajalapenoDecorator = new JalapenoDecorator(jalapenoDecorator);

            Console.WriteLine("Total price: {0}", extrajalapenoDecorator.GetPrice());

            IEmployeeRepository employeeRepository = new EmployeeRepository();

            IEmployeeRepository log = new Logger(employeeRepository);
            log.GetEmployees();

The last line of Code gets you the consolidated Price after adding the toppings. As you can see we are adding additional responsibilities to a base object during runtime through this pattern.



About these ads

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s