Decorator Design Pattern

The Decorator Design Pattern is one of the Structural Design Patterns. It allows you to add new behavior to an object dynamically without modifying its original class. Instead of extending functionality through inheritance, the Decorator pattern uses composition, where an object is wrapped inside one or more decorator objects that provide additional functionality.

Inheritance is fixed at compile time and can quickly lead to a large number of subclasses when multiple feature combinations are possible. For example, if a coffee shop offers Milk, Sugar, Chocolate, and Whipped Cream as optional add-ons, creating a separate subclass for every possible combination becomes impractical.

The Decorator pattern solves this problem by wrapping objects with decorators. Since decorators implement the same interface as the original object, they can be stacked together in any order, allowing behavior to be added or removed dynamically at runtime.

This document demonstrates two practical implementations of the Decorator Design Pattern in C++.

Example 1: Coffee Ordering System

A coffee shop allows customers to customize a basic coffee by adding ingredients such as milk and sugar.

Program


Explanation

Coffee is the Concrete Component that provides the basic beverage.

MilkDecorator and SugarDecorator are Concrete Decorators. Each wraps another IBeverage object and adds extra behavior without changing the original coffee class.

Multiple decorators can be stacked together, allowing unlimited combinations of add-ons.

Example 2: File Data Processing

A storage system writes data to disk. Depending on user requirements, the data can be compressed, encrypted, or both before being saved.

Program


Explanation

FileDataSource is the Concrete Component responsible for writing data.

CompressionDecorator compresses the data before forwarding it to the wrapped object.

EncryptionDecorator encrypts the data before forwarding it.

Because decorators share the same interface as the wrapped object, they can be chained together to build flexible processing pipelines.


Characteristics of the Decorator Design Pattern

PropertyDescription
Pattern TypeStructural Design Pattern
Core PrincipleAdd responsibilities to objects dynamically by wrapping them inside decorator objects.
Main ComponentsComponent, Concrete Component, Decorator, and Concrete Decorators.
RelationshipDecorators contain a reference or smart pointer to another Component object.
Primary BenefitNew functionality can be added without modifying existing classes, following the Open-Closed Principle.
Common ApplicationsGUI widgets, logging, compression, encryption, caching, streams, middleware, and optional product features.

Conclusion

The Decorator Design Pattern provides a flexible alternative to inheritance for extending object behavior. By wrapping objects with decorators that implement the same interface, new functionality can be layered dynamically at runtime. As demonstrated in the Coffee Ordering System and File Data Processing examples, the Decorator pattern keeps code modular, reusable, and scalable while avoiding the explosion of subclasses that inheritance often causes.