Skip to content

Files

Latest commit

79a4483 · Jan 6, 2025

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Jan 5, 2025
Jan 5, 2025
Jan 5, 2025
Jan 5, 2025
Jan 5, 2025
Jan 5, 2025
Jan 5, 2025
Jan 5, 2025
Jan 5, 2025
Jan 6, 2025
Jan 5, 2025
Jan 5, 2025
Jan 4, 2025

Decorator Pattern

The Decorator Pattern is a structural design pattern that allows behavior to be added to individual objects, dynamically, without affecting the behavior of other objects from the same class.


📖 What is the Decorator Pattern?

The Decorator Pattern wraps an object to add new behaviors or responsibilities. Each wrapper, or decorator, adds functionality to the base object while maintaining its interface.

Key features:

  1. Dynamic Behavior: Add responsibilities to objects dynamically at runtime.
  2. Composability: Combine multiple decorators to create complex behaviors.
  3. Open/Closed Principle: Extend an object's behavior without modifying its existing code.

🤔 Why Use the Decorator Pattern?

  1. Customization: Add new behavior to individual objects without altering the original class.
  2. Scalability: Combine multiple decorators for complex use cases.
  3. Flexibility: Avoid subclassing by using composition to add behaviors.

🔧 Implementation

The implementation of the Decorator Pattern can be found in:


🛠️ Example Usage

To see the Decorator Pattern in action, refer to the CoffeeHouse.java file. It demonstrates how to dynamically add condiments to beverages using decorators.


📊 UML Diagram

Loading
classDiagram
    class IObject {
        +stuff()
    }

    class Object {
        +stuff()
    }

    class Decorator {
        - object : IObject
        +stuff()
    }

    class ConcreteDecorator1 {
        +stuff()
    }

    class ConcreteDecorator2 {
        +stuff()
    }

    class ConcreteDecorator3 {
        +stuff()
    }

    IObject <|-- Object
    IObject <|-- Decorator
    Decorator <|-- ConcreteDecorator1
    Decorator <|-- ConcreteDecorator2
    Decorator <|-- ConcreteDecorator3
    Decorator o--> IObject

Note

If the UML above is not rendering correctly, you can view the diagram from the decorator_uml.png file.


📝 Key Takeaways

  • The Decorator Pattern is ideal for dynamically extending the behavior of objects.
  • It adheres to the Open/Closed Principle by allowing objects to be extended without modifying their code.
  • Use it when subclassing becomes impractical or leads to an explosion of classes.