DEV Community

Tomas Forsman
Tomas Forsman

Posted on

Decorator Pattern through Ice Cream

I'm working on a series of articles on the Decorator Pattern.
Any and all comments and feedback is highly appreciated.
This is the first part in the series.

What You Will Learn in This Article

  • Introduction to the Decorator Pattern: Gain a basic understanding of what the Decorator pattern is and why it's useful in software design.
  • Concrete Components: Learn about the primary elements like VanillaIceCream, which forms the core functionality we want to extend.
  • Decorators and Interfaces: Get introduced to the concept of decorators via the IceCreamDecorator class and understand the role of interfaces with Flavor.
  • Dynamic Behavior: See how the pattern allows for the dynamic modification of object properties without altering their structure, demonstrated through cost calculation.
  • Example Usage: End with a simple example to showcase how these pieces fit together in practice.

Introduction

The Decorator pattern is a structural design pattern used in software development to add new functionalities to objects without altering their existing structure. This pattern treats both the original objects and the decorated objects as instances of the same interface, making them interchangeable. Essentially, it allows you to "decorate" an object with additional features, without modifying its code. To visualize this, imagine adding toppings to a basic vanilla ice cream; each topping enhances the ice cream while keeping the original flavor intact.

Why It's Useful

  • Open/Closed Principle: The Decorator pattern adheres to the Open/Closed Principle, which states that software entities should be open for extension but closed for modification. You can extend the behavior of an object without modifying its code. This is like being able to add new ice cream toppings without having to create a new kind of ice cream altogether.
  • Flexibility: Instead of having a large monolithic class that handles different scenarios, you can break down functionalities into individual decorators. This allows you to mix and match behaviors at runtime, much like choosing different toppings for your ice cream.
  • Code Reusability and Maintenance: Smaller, specialized classes are easier to understand, debug, and reuse. When each decorator is responsible for a specific function, debugging is often simplified.
  • Composition Over Inheritance: The pattern offers an alternative to subclassing to extend functionalities. Subclassing adds features to a class at compile-time, whereas the decorator pattern adds them at runtime.
  • Clean Code: By segregating functionalities into distinct decorators, it makes your code more organized and adheres to the Single Responsibility Principle. In real-world applications, the Decorator pattern is commonly seen in GUI libraries, stream handling libraries, and even in some web development frameworks.

Real-World Analogy
Imagine you're standing in a quaint little ice cream shop. Your options range from exotic flavors like mango-chili to timeless classics like chocolate and strawberry. Today, you opt for the simple yet satisfying vanilla. This essential flavor is the base upon which countless other ice cream experiences can be built.
In software design, vanilla ice cream serves as our Concrete Component. It's straightforward and meets a specific need: satisfying your craving for ice cream. But suppose you wish to make your experience more exciting? On your next trip, you decide to add chocolate chips to your vanilla scoop.
Just like in the ice cream shop, the Decorator Pattern in software design allows your "vanilla" objects to be "topped" or "decorated" with additional features, like chocolate chips, without altering their core functionality. This sets the stage for more complex and versatile objects down the line.
By employing the Decorator Pattern, you create endless possibilities for future enhancements to your "vanilla" object. In this article, we'll begin by implementing our first topping, the "Chocolate Chip Decorator," to add another layer of functionality and flavor to our base object.
In the next article, we'll explore more "toppings" (or Decorators) that can add even more functionality and versatility to our basic "vanilla" objects.

Now, let us break the pattern apart into its core concepts.

Core Concepts in the Decorator Pattern

Interface

The Interface sets the standard for both Concrete Components and Decorators by specifying a common set of methods that they must implement. It serves as a contract that ensures the interchangeability of these elements within the system.

Concrete Component

The Concrete Component is an object that implements the Interface, providing the basic functionality that can be extended. This component serves as the foundational element upon which additional features, provided by decorators, can be built. It fully adheres to the contract stipulated by the Interface, making it compatible with any Decorator that also adheres to the same Interface.

Decorator

The Decorator class also implements the Interface but is designed to extend the functionalities of a Concrete Component. Unlike the Concrete Component, the Decorator holds a reference to a Concrete Component (or another Decorator) and delegates the core functionalities to this wrapped object. It then performs additional operations either before or after delegating to the original object's methods.

Relationship Among Core Concepts

  • Interface and Concrete Component: The Concrete Component is structured according to the Interface. This design makes it compatible with any object that also adheres to the Interface, ensuring system-wide consistency.
  • Interface and Decorator: Like the Concrete Component, the Decorator adheres to the Interface. This ensures that Decorators can be interchangeably used with any Concrete Component or even another Decorator.
  • Decorator and Concrete Component: The Decorator encapsulates a Concrete Component (or another Decorator), thus inheriting its properties and functionalities. This composition allows the Decorator to extend the functionalities of the Concrete Component without altering its structure. By adhering to a common Interface, both the Concrete Component and the Decorator can be used interchangeably, allowing for flexible and extensible design. The Decorator adds functionalities to the Concrete Component in a non-intrusive manner, adhering to the principles of single-responsibility and open-closed, thus promoting maintainable and scalable code.

Now, let’s look at these concept through the lens of our ice cream scenario.

Setting the Stage: The Flavor Interface
To kickstart our implementation of the Decorator Pattern, we'll first define a common interface that both our core ice cream flavor and its various toppings will implement. This interface will serve as a contract that enforces a certain structure on all the classes that implement it. In our case, we'll call this interface Flavor.
In our ice cream example, what's the one thing all flavors and toppings have in common? They all contribute to the final cost of the ice cream. So, we'll include a Cost() method in our Flavor interface that will return an integer representing the cost of that particular flavor or topping.
Here's what our Flavor interface looks like in pseudo code:

Interface Flavor:
Method Cost() -> Integer
Enter fullscreen mode Exit fullscreen mode

The Flavor interface declares a Cost() method that must be implemented by any class that adheres to this interface. This sets the stage for creating both the foundational ice cream flavor—our Concrete Component—and various toppings—our Decorators. Each of these will implement the Cost() method, ensuring that they conform to a common interface. This interchangeability is at the heart of the Decorator Pattern and allows us to add or remove features seamlessly.
By adhering to a common interface, we lay the groundwork for a design that is both flexible and extendable, keeping in line with the principles of clean code and effective software design. With our interface in place, we are ready to move on to the core flavor of our ice cream: vanilla.

Creating Our First Concrete Flavor: The VanillaIceCream Class

Now that we have set the stage with our Flavor interface, it's time to create the foundational element in our ice cream example: the VanillaIceCream class. This class serves as the Concrete Component in our Decorator Pattern. As the name suggests, a Concrete Component is a concrete implementation of our established interface, providing the fundamental functionalities that we can extend later on with Decorators.
In terms of our ice cream example, think of VanillaIceCream as the simple but satisfying vanilla scoop that serves as the base for adding various toppings. The class will implement the Flavor interface, specifically the Cost() method, which returns the basic cost of a vanilla ice cream.
Here's how our VanillaIceCream class looks in pseudo code:

Class VanillaIceCream implements Flavor:
Method Initialize():
this.base_cost = 10
Method Cost():
Return this.base_cost
Enter fullscreen mode Exit fullscreen mode

In this implementation, the VanillaIceCream class has an Initialize() method that sets the base_cost to 10. The Cost() method, adhering to our Flavor interface, returns this base cost. This makes VanillaIceCream a concrete class that fulfills the contract specified by the Flavor interface.
The VanillaIceCream class is simple but crucial. It lays the groundwork for future extensions. Without a well-defined Concrete Component, we can't have Decorators, because Decorators depend on something to decorate!
So, why are we starting with vanilla, you might ask? In the context of the Decorator Pattern, it's essential to start with a Concrete Component to establish the basic functionality that all subsequent Decorators will build upon. Our vanilla ice cream isn't just a flavor; it's a paradigm for the kinds of functionalities our system should offer.
Having set up our VanillaIceCream class as the Concrete Component, we're now ready to layer on additional functionality with Decorators.

Introducing Decorators: The IceCreamDecorator Class

With our Concrete Component (VanillaIceCream) in place, let's introduce the Decorator class that will help us extend functionalities dynamically. In the context of our ice cream example, a Decorator is like an optional topping you can add to the core vanilla scoop to enhance its features—whether it's adding chocolate chips, sprinkles, or even fruit slices.
In technical terms, a Decorator is a class that also implements the Flavor interface and is designed to extend the functionalities of a Concrete Component. It achieves this by holding a reference to a Concrete Component—or even another Decorator—and delegating the core functionalities to this wrapped object. After the delegation, it performs additional operations, thus extending the object's functionalities dynamically at runtime.
Here's how our IceCreamDecorator class is implemented in pseudo code:


Class IceCreamDecorator implements Flavor:
Method Initialize(ice_cream_flavor):
this.ice_cream_flavor = ice_cream_flavor
Method Cost():
Return this.ice_cream_flavor.Cost()
Enter fullscreen mode Exit fullscreen mode

In the above code, the IceCreamDecorator class takes an ice_cream_flavor object as a parameter during initialization. This object could be a Concrete Component like VanillaIceCream or another Decorator. The Cost() method in IceCreamDecorator then delegates to the Cost() method of the ice_cream_flavor object, effectively inheriting its cost and potentially adding more to it.
At this stage, IceCreamDecorator doesn't do much more than forwarding the Cost() method call to the object it wraps. It serves as a structural basis for more specialized Decorators, which will add actual functionalities.
For instance, if we create a ChocolateChipDecorator, it would extend IceCreamDecorator and add the cost of chocolate chips to the basic ice cream cost.
This approach ensures two important things:

  1. Extensibility: By using a common interface for both the Concrete Components and the Decorators, we can endlessly extend our objects with new features without having to modify existing code.
  2. Single Responsibility Principle: Each Decorator class will be responsible for a specific type of functionality, making the system easier to manage and scale. We'll explore a more specialized Decorator in the upcoming section, "Putting It All Together: The GetFinalIceCreamCost Function." There, we'll show how to make a more complex ice cream combination by stacking Decorators.

Putting It All Together: The GetFinalIceCreamCost Function

Having established our Concrete Component (VanillaIceCream) and a general-purpose Decorator (IceCreamDecorator), let's bring these elements together to achieve our ultimate goal: dynamically calculating the final cost of an ice cream with various toppings.
In a real-world software application, this is akin to orchestrating different functionalities to form a complex, feature-rich object dynamically at runtime. This is where the strength of the Decorator Pattern truly shines. You can combine different decorators in a highly flexible manner without altering the underlying code of the individual components involved.
For this example, let's say we want to calculate the cost of a vanilla ice cream decorated with chocolate chips.
The function GetFinalIceCreamCost is designed to bring these separate components together to calculate the total cost. This function first creates a VanillaIceCream object, which serves as the base or the Concrete Component. It then decorates this base with a ChocolateChipDecorator, which is our specialized Decorator.
Here's how it's done in pseudo code:

Function GetFinalIceCreamCost() -> Integer:
basicIceCream = new VanillaIceCream()
decoratedIceCream = new ChocolateChipDecorator(basicIceCream)
Return decoratedIceCream.Cost()
Enter fullscreen mode Exit fullscreen mode

In this function, the VanillaIceCream instance basicIceCream is initialized and passed to a new ChocolateChipDecorator instance called decoratedIceCream. Finally, we call the Cost() method on decoratedIceCream.
This is a simple but powerful example demonstrating how the Decorator Pattern allows for easy composition of behaviors. The Cost() method of the ChocolateChipDecorator will delegate to the Cost() method of VanillaIceCream, add its own extra cost for the chocolate chips, and return the final cost. In this way, we manage to extend the behavior of our VanillaIceCream object without altering its original implementation.
The function returns 12, which is 10 for the base vanilla flavor plus an additional 2 for chocolate chips.
This modular approach allows you to add as many specialized Decorators as needed, each contributing their own unique features to the final object. This is an excellent demonstration of adhering to principles like Open/Closed and Single Responsibility, making your codebase flexible, extensible, and maintainable.
Full example in pseudo code

// Interface that defines a common contract for all ice cream flavors.
// The Cost method returns the financial cost of a specific ice cream flavor.
Interface Flavor:
Method Cost() -> Integer

// The VanillaIceCream class is a concrete implementation of the Flavor interface.
// It serves as the Concrete Component in the Decorator Pattern, providing foundational functionality.
Class VanillaIceCream implements Flavor:
// Initializes the VanillaIceCream class with a base cost value.
Method Initialize():
this.base_cost = 10
// Returns the base cost of the vanilla ice cream.
Method Cost():
Return this.base_cost

// The IceCreamDecorator class provides a framework for extending the features of existing ice cream flavors.
// It also implements the Flavor interface to ensure compatibility.
Class IceCreamDecorator implements Flavor:
// Constructor that accepts an ice cream flavor to be decorated.
Method Initialize(ice_cream_flavor):
this.ice_cream_flavor = ice_cream_flavor
// Returns the cost of the decorated ice cream by delegating to the original ice cream flavor's Cost method.
Method Cost():
Return this.ice_cream_flavor.Cost()

// The ChocolateChipDecorator class is a specific type of ice cream decorator.
// It extends the general-purpose IceCreamDecorator class.
Class ChocolateChipDecorator extends IceCreamDecorator:
// Constructor initializes the base decorator and sets the additional cost for chocolate chips.
Method Initialize(ice_cream_flavor):
super.Initialize(ice_cream_flavor)
this.extra_cost = 2
// Calculates and returns the total cost, which includes the base cost and the extra cost for chocolate chips.
Method Cost():
Return super.Cost() + this.extra_cost

// Main function that demonstrates how to dynamically calculate the final cost of an ice cream flavor with toppings.
// It applies the Decorator Pattern to assemble a vanilla ice cream with chocolate chips and then calculates its cost.
Function GetFinalIceCreamCost() -> Integer:
// Create an instance of VanillaIceCream, serving as the base flavor.
basicIceCream = new VanillaIceCream()
// Decorate the vanilla ice cream with chocolate chips.
decoratedIceCream = new ChocolateChipDecorator(basicIceCream)
// Calculate and return the final cost of the decorated ice cream.
Return decoratedIceCream.Cost()  // Outputs 12 (10 for Vanilla + 2 for Chocolate Chips)
Enter fullscreen mode Exit fullscreen mode

Summary

In this first installment of our series, we've peeled back the layers of the Decorator pattern to understand its core principles. Utilizing a relatable ice cream example, we’ve journeyed from simple vanilla to a more elaborate, chocolate chip-topped treat. We explored how to define an interface, implement a concrete class, and extend functionalities through decorators, all while adhering to software design principles like the Open/Closed Principle and the Single Responsibility Principle.

What's Next

The road ahead is sprinkled with even more exciting features and nuances to add to our Decorator pattern. In the next article, titled "Enhancing Flavors with Toppings," we'll scale up the complexity by introducing more decorators. We'll explore how to layer multiple features on top of our base ice cream flavor, creating endless combinations to suit any palate.
Beyond that, we'll generalize our approach, expanding from ice creams to any product with a cost. This will provide a scalable and adaptable framework that can be employed across a myriad of scenarios. We'll also delve into the mechanics of parameterized decorators, robust error handling, and dynamic user choice integration.
So, strap in for an enlightening journey through the world of design patterns. The Decorator pattern has much more to offer, and we're just scratching the surface.
Stay tuned!

**
Additional Resources**
The Decorator pattern is a topic well-covered in literature and online resources. Here's a list that could greatly enhance your understanding:
Books

  • "Design Patterns: Elements of Reusable Object-Oriented Software" by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (aka the "Gang of Four"): This seminal book introduced the concept of design patterns, including the Decorator pattern, and is considered a must-read for any serious software developer.
  • "Head First Design Patterns" by Eric Freeman and Elisabeth Robson: This book offers an easier-to-digest look at design patterns, including the Decorator pattern, making it accessible for those new to the topic.
  • "Clean Code: A Handbook of Agile Software Craftsmanship" by Robert C. Martin: Though not exclusively about design patterns, this book talks about code organization and principles that relate well to the use of patterns like Decorator.
  • "Patterns of Enterprise Application Architecture" by Martin Fowler: This book provides an in-depth look into enterprise patterns, including the Decorator pattern, and offers practical examples for real-world implementation.

Websites

Online Courses

YouTube Channels

  • Academind
    • Frequently covers design patterns, including the Decorator pattern, in various programming languages.
  • The Coding Train
    • Offers easy-to-understand video tutorials that include various design patterns. These resources offer varying degrees of depth and perspective, so you're likely to find something that suits your learning style.

Top comments (0)