Decorator Pattern

The Decorator Pattern provides a flexible alternative to sub classing for extending functionality dynamically.
The idea of the Decorator Pattern is to wrap an existing class, add other functionality to it, then expose the same interface to the outside world. Because of this our decorator exactly looks like the original class to the people who are using it.
It is used to extend or alter the functionality at runtime. It does this by wrapping them in an object of the decorator class without modifying the original object. So it can be called a wrapper pattern.
Advantages of Decorator Pattern
- Adds functionality to existing objects dynamically
- Alternative to sub classing
- Flexible design
- Supports Open Closed Principle
When to use Decorator Pattern
- Legacy System
- Contorls
- Sealed Classes
The following class diagram shows us the Decorator Pattern's design:

- Component: It defines the interface of the actual object that needs functionality to be added dynamically to the ConcreteComponents.
- ConcreteComponent: The actual object in which the functionalities could be added dynamically.
- Decorator: This defines the interface for all the dynamic functionalities that can be added to the ConcreteComponent.
- ConcreteDecorator: All the functionalities that can be added to the ConcreteComponent. Each needed functionality will be one ConcreteDecorator class.
A decorated class is a base class of concrete decorator classes and it inherits from the base component class. So the decorators can be used in the place of concrete components.
Let's start learning the Decorator Pattern with one real time problem. Suppose we need to build an application for a car show room that sells cars along with car accessories. Economy, Deluxe and Luxury are the car categories that come with the optional accessories packages Basic, Advanced and Sport. So the application must provide the car cost adding the cost of accessory package if the user chooses any of the preceding packages.
Okay. Let's start designing our application with the ICar interface that will be implemented by Economy, Deluxe and Luxury Car category classes.
- /// <summary>
- /// Car Base component
- /// </summary>
- public interface ICar
- {
- string GetDescription();
- double GetCost();
- }
- /// <summary>
- /// Concrete Car
- /// </summary>
- public class EconomyCar : ICar
- {
- public string GetDescription()
- {
- return "Economy Car";
- }
- public double GetCost()
- {
- return 450000.0;
- }
- }
- /// <summary>
- /// Concrete Car
- /// </summary>
- public class DeluxCar : ICar
- {
- public string GetDescription()
- {
- return "Delux Car";
- }
- public double GetCost()
- {
- return 750000.0;
- }
- }
- /// <summary>
- /// Concrete Car
- /// </summary>
- public class LuxuryCar : ICar
- {
- public string GetDescription()
- {
- return "Luxury Car";
- }
- public double GetCost()
- {
- return 1000000.0;
- }
- }
- public class EconomyCarWithBasicAccessories: ICar{
- ..
- ..
- }
- public class DeluxCarWithBasicAccessories: ICar{
- ..
- ..
- }
- public class LuxuryCarWithBasicAccessories: ICar{
- ..
- ..
- }
- ..
- ..
- ..
- ..
- public class EconomyCarWithSportAccessories: ICar{
- ..
- ..
- }
- public class DeluxCarWitSportAccessories: ICar{
- ..
- ..
- }
- public class LuxuryCarWithSportAccessories: ICar{
- ..
- ..
- }


Chu DuPosted Sep 23, 2023, 1:07 AM
Thank, very nice.