Advanced Java September 20 ,2025

Decorator Design Pattern in Java

Introduction

In software development, there are situations where you want to add new functionality to objects dynamically without modifying their original code. The Decorator Pattern is a structural design pattern that provides a flexible alternative to subclassing for extending functionality.

Think of it like customizing a coffee order: you start with a basic coffee and dynamically add milk, sugar, or chocolate toppings without altering the base coffee class.

Definition

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

Key Idea: Wrap objects with decorator classes that enhance or modify behavior.

When to Use the Decorator Pattern

  • When you need to add responsibilities to objects at runtime.
  • When subclassing would result in an explosion of classes for every combination of behaviors.
  • When you want to extend object functionality in a flexible and reusable way.

Real-World Example

  • Coffee Shop: Base coffee can have milk, sugar, whipped cream, or chocolate toppings. Each topping can be applied independently.
  • Graphical User Interfaces (GUI): A TextView can have borders, scrollbars, or shadows added dynamically using decorators.

Java Implementation

Let’s implement a coffee shop example using the Decorator Pattern.

Step 1: Define the Component Interface

// Component interface
interface Coffee {
    String getDescription();
    double getCost();
}

Step 2: Create the Concrete Component

// Concrete Component
class SimpleCoffee implements Coffee {
    @Override
    public String getDescription() {
        return "Simple Coffee";
    }

    @Override
    public double getCost() {
        return 5.0;
    }
}

Step 3: Define the Decorator Abstract Class

// Decorator abstract class
abstract class CoffeeDecorator implements Coffee {
    protected Coffee coffee;  // The object being decorated

    public CoffeeDecorator(Coffee coffee) {
        this.coffee = coffee;
    }

    public String getDescription() {
        return coffee.getDescription();  // Delegate to wrapped object
    }

    public double getCost() {
        return coffee.getCost();  // Delegate to wrapped object
    }
}

Step 4: Create Concrete Decorators

// Milk decorator
class MilkDecorator extends CoffeeDecorator {
    public MilkDecorator(Coffee coffee) {
        super(coffee);
    }

    @Override
    public String getDescription() {
        return coffee.getDescription() + ", Milk";
    }

    @Override
    public double getCost() {
        return coffee.getCost() + 1.5;
    }
}

// Sugar decorator
class SugarDecorator extends CoffeeDecorator {
    public SugarDecorator(Coffee coffee) {
        super(coffee);
    }

    @Override
    public String getDescription() {
        return coffee.getDescription() + ", Sugar";
    }

    @Override
    public double getCost() {
        return coffee.getCost() + 0.5;
    }
}

Step 5: Test the Decorator Pattern

public class Main {
    public static void main(String[] args) {
        Coffee simpleCoffee = new SimpleCoffee();
        System.out.println(simpleCoffee.getDescription() + " → $" + simpleCoffee.getCost());

        Coffee milkCoffee = new MilkDecorator(simpleCoffee);
        System.out.println(milkCoffee.getDescription() + " → $" + milkCoffee.getCost());

        Coffee milkSugarCoffee = new SugarDecorator(milkCoffee);
        System.out.println(milkSugarCoffee.getDescription() + " → $" + milkSugarCoffee.getCost());
    }
}

Output:

Simple Coffee → $5.0
Simple Coffee, Milk → $6.5
Simple Coffee, Milk, Sugar → $7.0

Advantages of the Decorator Pattern

  1. Adds Flexibility: Modify object behavior at runtime without altering the base class.
  2. Reduces Class Explosion: Avoid creating multiple subclasses for each combination of features.
  3. Promotes Open/Closed Principle: Classes are open for extension but closed for modification.
  4. Enhances Reusability: Decorators can be combined in different ways to achieve varied behavior.

Disadvantages

  1. Complexity: Multiple layers of wrapping can make the system harder to understand.
  2. Debugging Difficulty: Tracing decorated objects can be challenging.
  3. Overhead: Each decorator introduces additional object references, which may slightly impact performance.

Summary

The Decorator Pattern is ideal when you need dynamic behavior modification of objects. By wrapping objects with decorators, it allows flexible, reusable, and extensible design without altering existing code.

Next, we will explore the Facade Pattern, which provides a simplified interface to a complex subsystem, making it easier for clients to interact with large systems.

 

 

Sanjiv
0

You must logged in to post comments.

Related Blogs

Generics P...
Advanced Java August 08 ,2025

Generics Part- 2

Collection...
Advanced Java July 07 ,2025

Collections Framewor...

Mastering...
Advanced Java August 08 ,2025

Mastering Java Multi...

Annotation...
Advanced Java August 08 ,2025

Annotations

Java Memor...
Advanced Java August 08 ,2025

Java Memory Manageme...

Java Lambd...
Advanced Java August 08 ,2025

Java Lambda Expressi...

Java Funct...
Advanced Java August 08 ,2025

Java Functional Inte...

Java Strea...
Advanced Java August 08 ,2025

Java Stream API

JDBC (Java...
Advanced Java August 08 ,2025

JDBC (Java Database...

JDBC (Java...
Advanced Java September 09 ,2025

JDBC (Java Database...

Annotation...
Advanced Java August 08 ,2025

Annotations

Generics
Advanced Java August 08 ,2025

Generics

Java I/O (...
Advanced Java August 08 ,2025

Java I/O (NIO)

Introducti...
Advanced Java September 09 ,2025

Introduction to Desi...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Other Prin...
Advanced Java September 09 ,2025

Other Principles Beh...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Builder De...
Advanced Java September 09 ,2025

Builder Design Patte...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Chain of R...
Advanced Java September 09 ,2025

Chain of Responsibil...

Command De...
Advanced Java September 09 ,2025

Command Design Patte...

Interprete...
Advanced Java September 09 ,2025

Interpreter Design P...

Iterator D...
Advanced Java September 09 ,2025

Iterator Design Patt...

Mediator D...
Advanced Java September 09 ,2025

Mediator Design Patt...

Memento De...
Advanced Java September 09 ,2025

Memento Design Patte...

Observer D...
Advanced Java September 09 ,2025

Observer Design Patt...

State Desi...
Advanced Java September 09 ,2025

State Design Pattern...

Strategy D...
Advanced Java September 09 ,2025

Strategy Design Patt...

Template M...
Advanced Java September 09 ,2025

Template Method Desi...

Visitor De...
Advanced Java September 09 ,2025

Visitor Design Patte...

Prototype...
Advanced Java September 09 ,2025

Prototype Design Pat...

Java 8+ Fe...
Advanced Java October 10 ,2025

Java 8+ Features

SOLID Prin...
Advanced Java October 10 ,2025

SOLID Principles in...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

How Iterat...
Advanced Java October 10 ,2025

How Iterators Work i...

How Concur...
Advanced Java October 10 ,2025

How ConcurrentHashMa...

Comparable...
Advanced Java October 10 ,2025

Comparable vs Compar...

Get In Touch

G06, Kristal Olivine Bellandur near Bangalore Central Mall, Bangalore Karnataka, 560103

+91-8076082435

techiefreak87@gmail.com