Published Date: 05 February, 2022

Overview : Decorator Pattern

Decorator pattern is a part of Structure Design Pattern and it allows to add functionality to an individual object without affecting the other object of the same class. It is aligned with the Single Responsibility principal of the SOLID principal.

Basically, you create class for each behavior/functionality that can be use independently to add behavior on the given object.

Let's take an example to understand this better. Suppose you a very basic Pizza that can be eaten. Here you can give the options to your end customers that they can choose different kinds of toppings to make the basic Pizza more delicious. For example, option can be to add cheese or extra cheese or chicken etc. Even your end customer can choose multiple toppings as well. For example, customer may want to go with extra cheese and chicken both toppings.

If you see the above example, you need to define concreate basic object (basic Pizza), the operation (adding toppings), and behaviors (different kinds of toppings). Let's form this in the form of OOPS.

First need to define the interface that will have all the allowed operations.

public interface IDecorator
{

string Content { get; set; }
void Operation();

}

Now need to define the concreate class that will keep the basic object behavior.

public class ConcreateClass : IDecorator
{

public string Content { get; set; }
public ConcreateClass()
{
}

public void Operation()
{

Content = "BasePizza ";
Console.WriteLine(Content);
}

}

Now we need to create a decorator class. Every topping class will inherit this decorator class.

public abstract class Decorator: IDecorator
{

private IDecorator _decorator { get; set; }
public string Content { get ; set ; }

public Decorator(IDecorator decorator)
{

_decorator = decorator;
Content = decorator.Content;

}

public virtual void Operation()
{

_decorator.Operation();

}

}

Now the times comes to create toppings class (behavior/functionality that would like to have).

public class ToppingCheese : Decorator
{

public ToppingCheese(IDecorator decorator) : base(decorator){}

public override void Operation()
{

Content = string.Concat(Content, "-> ToppingCheese ");
Console.WriteLine(Content);

}

}

public class ToppingExtraCheese : Decorator
{

public ToppingExtraCheese(IDecorator decorator) : base(decorator){}

public override void Operation()
{

Content = string.Concat(base.Content, "-> ToppingExtraCheese ");
Console.WriteLine(Content);

}

}
public class ToppingChicken : Decorator
{

public ToppingChicken(IDecorator decorator) : base(decorator){}

public override void Operation()
{

Content = string.Concat(Content, "-> ToppingChiken ");
Console.WriteLine(Content);

}

}

Now you are ready with your decorator pattern. It's time to use this code.

http://www.tekkishare.com

You can see here, first you have created an object of your base class (BasePizza) and you can use this without any decorator. But it has a very basic functionality.

You can add any toppings/behavior using the base object that will have baseObject and the topping that you added.

If you run this application, you will see the following output.

http://www.tekkishare.com

You can see here; your customer can order base pizza with single toppings or multiple toppings without affecting the current object.



References:

Most Recent Post

DECORATOR

OBSERVER

Subscription
Tags
C-Sharp Design-Pattern