DEV Community

Cover image for Demystifying the Decorator Design Pattern in TypeScript with Coffee Brewing
Vedant Bhamare
Vedant Bhamare

Posted on

Demystifying the Decorator Design Pattern in TypeScript with Coffee Brewing

If you're new to software design patterns, you might have come across the term "Decorator." It's one of the fundamental design patterns that fall under the category of structural patterns. In this blog post, we'll explore the Decorator Design Pattern in TypeScript, using a simple and relatable example: a coffee brewing system. We'll break down the concept step by step to make it easy for beginners to understand.

*What is the Decorator Design Pattern?*

The Decorator Design Pattern is a structural pattern that allows you to add new behaviors or responsibilities to an object dynamically without altering its structure. In simpler terms, it helps you add or modify functionalities to an object without changing its core essence. We will be going through the “Coffee Brewing System” example for better understanding of the Decorator Design Pattern.

*Key Concepts and Components:*

  1. Component: This is the interface or abstract class that defines the methods that will be common to both the base object and the decorators. In the coffee example, SimpleCoffee is the component, defining methods like cost() and description().
  2. Concrete Component: This is the base class that implements the Component interface. In the coffee example, SimpleCoffee is the concrete component.
  3. Decorator: This is an abstract class or interface that also implements the Component interface. It serves as the base class for all concrete decorators. In the coffee example, MilkDecorator, SugarDecorator, and CaramelDecorator are decorators.
  4. Concrete Decorator: These are the classes that extend the Decorator class and add specific functionalities or behaviors. In the coffee example, MilkDecorator, SugarDecorator, and CaramelDecorator are concrete decorators.

*How the Decorator Pattern Works:*

  1. Composition: The key idea behind the Decorator pattern is composition. Instead of adding functionalities directly to a class, you wrap it with one or more decorator classes. Each decorator has a reference to the component it decorates.
  2. Chainability: Decorators can be stacked or chained together. You can add one or more decorators to an object, creating a chain of responsibilities. Each decorator adds its own behavior and can pass the request to the next decorator in the chain.
  3. Transparent to Clients: From the client's perspective, it's unaware of the decorators and treats the decorated object just like the original component. This makes it easy to use and doesn't clutter the client code with conditional checks for specific behaviors.
  4. Dynamic Behavior: You can dynamically add or remove decorators at runtime. This flexibility allows you to change an object's behavior without altering its class.

*Let's Brew Some Coffee!*

To understand the Decorator pattern better, let's create a coffee brewing system in TypeScript. Our base object will be a simple coffee, and we'll use decorators to add various condiments.

*Step 1: Define the base coffee class*

class SimpleCoffee {
  cost() {
    return 5; // Base cost of a simple coffee
  }

  description() {
    return "Simple Coffee";
  }
}
Enter fullscreen mode Exit fullscreen mode

In this step, we define a SimpleCoffee class, which serves as the base coffee. It has two methods:

  • cost(): This method returns the base cost of a simple coffee, which is $5.
  • description(): This method returns the description of a simple coffee, which is "Simple Coffee."

*Step 2: Create a decorator class for milk*

class MilkDecorator {
  constructor(private coffee: SimpleCoffee) {}

  cost() {
    return this.coffee.cost() + 2; // Adding the cost of milk
  }

  description() {
    return `${this.coffee.description()}, Milk`;
  }
}
Enter fullscreen mode Exit fullscreen mode

In this step, we create a MilkDecorator class, which is a decorator for adding milk to a coffee. It takes a SimpleCoffee object as a parameter in its constructor.

  • cost(): This method calculates the cost of the coffee with milk by adding $2 to the cost of the base coffee.
  • description(): This method adds "Milk" to the description of the coffee.

*Step 3: Create a decorator class for sugar*

class SugarDecorator {
  constructor(private coffee: SimpleCoffee) {}

  cost() {
    return this.coffee.cost() + 1; // Adding the cost of sugar
  }

  description() {
    return `${this.coffee.description()}, Sugar`;
  }
}
Enter fullscreen mode Exit fullscreen mode

Similar to the MilkDecorator, we create a SugarDecorator class to add sugar to a coffee.

  • cost(): This method calculates the cost of the coffee with sugar by adding $1 to the cost of the base coffee.
  • description(): This method adds "Sugar" to the description of the coffee.

*Step 4: Create a decorator class for caramel syrup*

class CaramelDecorator {
  constructor(private coffee: SimpleCoffee) {}

  cost() {
    return this.coffee.cost() + 3; // Adding the cost of caramel syrup
  }

  description() {
    return `${this.coffee.description()}, Caramel Syrup`;
  }
}
Enter fullscreen mode Exit fullscreen mode

The CaramelDecorator class is another decorator, this time for adding caramel syrup to a coffee.

  • cost(): This method calculates the cost of the coffee with caramel syrup by adding $3 to the cost of the base coffee.
  • description(): This method adds "Caramel Syrup" to the description of the coffee.

*Putting it All Together*

Now, let's use these classes to create and decorate coffee objects:

// Create a simple coffee
const coffee = new SimpleCoffee();

// Add milk to the coffee
const coffeeWithMilk = new MilkDecorator(coffee);

// Add sugar to the coffee with milk
const coffeeWithMilkAndSugar = new SugarDecorator(coffeeWithMilk);

// Add caramel syrup to the coffee with milk and sugar
const coffeeWithMilkSugarAndCaramel = new CaramelDecorator(coffeeWithMilkAndSugar);
Enter fullscreen mode Exit fullscreen mode

Output:

Image description

With these steps, we can create different combinations of coffee by stacking decorators. Each decorator adds its own behavior to the base coffee, allowing us to customize our coffee orders without modifying the original SimpleCoffee class.

*Advantages of the Decorator Pattern:*

  • Open-Closed Principle: The Decorator pattern adheres to the open-closed principle, which means you can extend the behavior of a class without modifying its source code. This promotes code stability and maintainability.
  • Reusable Decorators: Decorators are reusable components. You can mix and match them to create various combinations of behaviors, making your code more versatile.
  • Single Responsibility Principle: Each decorator class has a single responsibility, which makes the code easier to understand and maintain.

*When to Use the Decorator Pattern:*

Use the Decorator pattern when you want to:

  • Add responsibilities to objects dynamically.
  • Avoid class explosion (creating many subclasses for each combination).
  • Keep classes open for extension but closed for modification.
  • Compose objects with different behaviors without cluttering client code.

The Decorator pattern is a powerful tool for building flexible and extensible systems. It promotes clean code by separating concerns and enhancing objects without breaking existing functionality. Understanding and applying this pattern can significantly improve your software design skills.

Common *Use Cases for the Decorator Pattern:*

  1. GUI Widgets: Adding borders, scrollbars, and other visual enhancements to GUI widgets
  2. I/O Streams: Adding buffering, encryption, or compression to streams
  3. Text Formatting: Applying different formatting options (bold, italic, underline) to text
  4. Logging: Enhancing log entries with timestamps, severity levels, or additional information
  5. Authentication and Authorization: Adding authentication and authorization checks to methods or components
  6. Caching: Wrapping data retrieval methods with caching logic

Conclusion:

In this blog post, we delved into the world of the Decorator Design Pattern, using a relatable example of a coffee brewing system in TypeScript. By exploring this pattern step by step, we've demystified the concept for beginners and highlighted its significance in software design.

The Decorator Design Pattern empowers developers to enhance objects dynamically without altering their core structure.

Whether you're building a coffee shop simulator or designing complex software systems, understanding and applying the Decorator Design Pattern can significantly improve the maintainability and extensibility of your codebase. It's a powerful tool in the software developer's toolkit, offering a versatile way to create objects with dynamic behaviors.

So, the next time you're faced with the challenge of adding new features or responsibilities to objects, remember the Decorator pattern. It's your key to keeping your codebase open for extension while closed for modification, and it can make your software design endeavors a whole lot smoother and more elegant. Happy coding!

Top comments (0)