Friday, 20 December 2013

Decorator Design pattern - Real Time Example [Dosa]

🥞 Master Java & Structural Design Patterns!

Enjoying real-world coding analogies? Subscribe to Ram N Java for clean code, design architecture insights, and beginner-friendly programming lessons.

🔔 Subscribe to Ram N Java

What is the Decorator Design Pattern?

The Decorator Pattern is a structural design pattern that allows you to add new behaviors and responsibilities to individual objects dynamically at runtime without altering their underlying code.

Instead of relying on deep and rigid inheritance trees, this pattern favors composition over inheritance. It wraps existing objects inside flexible decorator objects that share the exact same interface.

Real-Time Analogy: The South Indian Dosa

Think of ordering food at a South Indian restaurant:

  • Every order starts with a standard, freshly prepared Plain Dosa.
  • One customer wants it decorated with spiced potato filling to make a Masala Dosa.
  • Another customer requests a topping of chopped onions to make an Onion Dosa.
  • Someone else might ask for both onions and masala together!

If you tried modeling this using subclassing, you would need dedicated classes for every combination: PlainDosa, MasalaDosa, OnionDosa, OnionMasalaDosa, etc. This causes class explosion and makes future additions tedious.

With the Decorator Pattern, each topping acts as an independent decorator wrapping the base dosa dynamically!

How the Dosa Decorator Architecture Works

1. Component Interface

Defines the baseline contract that both the basic dosa and any toppings must follow:

public interface Dosa {
    String makeDosa();
}

2. Concrete Component

Provides the core baseline implementation:

public class PlainDosa implements Dosa {
    @Override
    public String makeDosa() {
        return "Plain Dosa";
    }
}

3. Abstract Decorator (Wrapper)

Implements Dosa and holds a reference to an existing Dosa object:

public abstract class DosaDecorator implements Dosa {
    protected Dosa dosa;

    public DosaDecorator(Dosa dosa) {
        this.dosa = dosa;
    }

    @Override
    public String makeDosa() {
        return dosa.makeDosa();
    }
}

4. Concrete Decorators (Toppings)

Extend the decorator to enhance functionality dynamically:

public class OnionDosaDecorator extends DosaDecorator {
    public OnionDosaDecorator(Dosa dosa) {
        super(dosa);
    }

    @Override
    public String makeDosa() {
        return dosa.makeDosa() + " + Chopped Onions";
    }
}

public class MasalaDosaDecorator extends DosaDecorator {
    public MasalaDosaDecorator(Dosa dosa) {
        super(dosa);
    }

    @Override
    public String makeDosa() {
        return dosa.makeDosa() + " + Potato Masala";
    }
}

Key Takeaways & Benefits

  • Open/Closed Principle: New varieties or toppings can be introduced without changing existing code.
  • Dynamic Nesting: Toppings can be stacked and layered at runtime in any combination.
  • Clean Modularity: Responsibilities stay decoupled, avoiding bloated inheritance structures.

Related Tutorials You Might Like

Deepen your software design expertise with these related lessons from the channel:


Click here to watch in Youtube : https://www.youtube.com/watch?v=YDyuD7PHC_c

Click the below Image to Enlarge
Decorator Design pattern - Real Time Example [Dosa]
























See also:

  • Decorator Design pattern - Introduction
  • Decorator Design pattern - Real Time Example [Ice Cream]
  • Decorator Design pattern - Real Time Example [Pizza]
  • Decorator Design pattern - Real Time Example [Car]
  • Decorator Design pattern - Class Diagram
  • Decorator Design pattern - Sequence Diagram
  • Decorator Design pattern - Implementation [Dosa]
  • Decorator Design pattern - Implementation [Pizza]
  • Decorator Design pattern - Implementation [Ice Cream]
  • Decorator Design pattern - Implementation [Car]
  • Decorator Design pattern - Implementation [Shape]
  • Decorator Design pattern - Key Points
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials