đź“…Week-3 (Day-3) - Decorator Pattern Explained | Real-world use case + Code | Design patterns in LLD

Payal KumariPayal Kumari
4 min read

NOTE: - I started my 8-week system design journey with Coder Army. I will be journaling every day, recording what I learn, reflecting on it, and sharing it with my network to help newcomers to system design.

đź’ What is the Decorator Pattern?

The Decorator Pattern allows you to dynamically add behavior or responsibilities to objects without modifying their code.

🎯 It gives you a flexible alternative to subclassing, perfect when you want to extend functionalities at runtime.

Decorator Pattern ek aisa design hai jisme aap kisi object ke upar naye features ya behavior add kar sakte ho bina uske original code ko chhue.

(Hindi : Jaise ek aadmi ne jacket pehna, phir topi lagayi, phir sunglasses — ek ke baad ek cheezein add hoti gayi!)

đź’ Real-life Analogy: Mario Game

Imagine Mario, the classic video game character:

  • Base Mario: Can just walk.

  • Eats a Mushroom → Grows taller.

  • Eats a Fire Flower → Can shoot fireballs.

  • Gets a Star → Becomes invincible.

👉 Mario’s core abilities stay the same, but new powers are added on top, dynamically — just like decorators!

đź’ How Does It Work?

We use:

  • Component Interface (IComponent): Base functionalities

  • Concrete Component (ConcreteComponent): Real object

  • Decorator Class: Abstract wrapper

  • Concrete Decorators: Extra powers

đź’ Why & When to Use the Decorator Pattern?

âś… Use When:

  • You want to add behavior to objects at runtime.

  • You don’t want to modify original code.

  • You need a flexible way to extend functionality.

❌ Don’t Use When:

  • Too many layers might make debugging hard.

  • Can lead to too many small classes.

đź’  Difference Between Inheritance vs Decorator

FeatureInheritanceDecorator
Compile-timeBehavior added at compile-timeBehavior added at run-time
Tight couplingYesNo (more flexible)
Open-Closed❌ May violate it✅ Follows open-closed principle
// Component Interface: defines a common interface for Mario and all power-up decorators.
interface Character {
    String getAbilities();
}

// Concrete Component: Basic Mario character with no power-ups.
class Mario implements Character {
    public String getAbilities() {
        return "Mario";
    }
}

// Abstract Decorator: CharacterDecorator "is-a" Character and "has-a" Character.
abstract class CharacterDecorator implements Character {
    protected Character character;  // Wrapped component

    public CharacterDecorator(Character c) {
        this.character = c;
    }
}

// Concrete Decorator: Height-Increasing Power-Up.
class HeightUp extends CharacterDecorator {
    public HeightUp(Character c) {
        super(c);
    }

    public String getAbilities() {
        return character.getAbilities() + " with HeightUp";
    }
}

// Concrete Decorator: Gun Shooting Power-Up.
class GunPowerUp extends CharacterDecorator {
    public GunPowerUp(Character c) {
        super(c);
    }

    public String getAbilities() {
        return character.getAbilities() + " with Gun";
    }
}

// Concrete Decorator: Star Power-Up (temporary ability).
class StarPowerUp extends CharacterDecorator {
    public StarPowerUp(Character c) {
        super(c);
    }

    public String getAbilities() {
        return character.getAbilities() + " with Star Power (Limited Time)";
    }
}

public class DecoratorPattern {
    public static void main(String[] args) {
        // Create a basic Mario character.
        Character mario = new Mario();
        System.out.println("Basic Character: " + mario.getAbilities());

        // Decorate Mario with a HeightUp power-up.
        mario = new HeightUp(mario);
        System.out.println("After HeightUp: " + mario.getAbilities());

        // Decorate Mario further with a GunPowerUp.
        mario = new GunPowerUp(mario);
        System.out.println("After GunPowerUp: " + mario.getAbilities());

        // Finally, add a StarPowerUp decoration.
        mario = new StarPowerUp(mario);
        System.out.println("After StarPowerUp: " + mario.getAbilities());
    }
}

đź’  Real-World Example

📍Gift Wrapping Analogy:

  • Gift = Base object.

  • Wrapper = Decorator.

  • Add ribbon, cover, bow — all without changing the gift itself!

📍 Zomato Order:

  • You order food (base).

  • Add cutlery, sauces, extra cheese (decorators).

Week - 3 (Day 3) Completed âś… System Design

NOTE : - A big thanks to my mentors Rohit Negi Sir and Aditya Sir for launching this amazing 8-week course absolutely free on YouTube via CoderArmy9 :- youtube.com/@CoderArmy9 . 🙌

👉 Share this blog with your connections! Let’s keep learning, growing, and supporting one another on this journey. 🚀

✍️ Payal Kumari 👩‍💻 Github

Jai Hind 🇮🇳 | #CoderArmy #LearningInPublic #SystemDesign #TechForAll #MentorshipMatters #8weeksLLdChallenge #LowLevelDesign #LLD 👩‍💻

0
Subscribe to my newsletter

Read articles from Payal Kumari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Payal Kumari
Payal Kumari

I'm a passionate full-stack developer with a strong foundation in the MERN stack—building and maintaining scalable web applications using React.js, Node.js, and Next.js. My journey in open source began with Hacktoberfest 2023, where I made four impactful pull requests that sparked a love for collaborative coding, global learning, and open knowledge sharing. Since then, I’ve contributed to and mentored projects in top open source programs like GSSoC’24, SSOC’24, and C4GT’24. As a Google Gen AI Exchange Hackathon ’24 Finalist and Google’s Women Techmakers (WTM) Ambassador, I’ve been privileged to support diverse communities in building meaningful tech solutions. My work as a Top 50 Mentor for GSSoC ’24 reflects my commitment to nurturing new talent in tech. Beyond development, I serve as a Student Career Guide, Profile Building Expert & Evangelist at Topmate.io, where I conduct workshops, guide students through resume building and career strategy, and help mentees navigate open source and tech careers. Recognized among the Top 5% of mentors and featured on “Topmate Discover,” I take pride in making mentorship accessible and impactful. My technical voice has also been acknowledged by LinkedIn, where I’ve earned the Top Voice badge seven times in domains like web development, programming, and software engineering. In addition, I hold LinkedIn Golden Badges for Research Skills, Interpersonal Skills, Critical Thinking, and Teamwork—signaling a well-rounded approach to both individual contribution and team collaboration. Graduating with an MCA from Chandigarh University in 2023, I’ve continued to fuel my curiosity by writing technical articles and sharing practical MERN stack insights across platforms. Whether it’s building polished UIs, optimizing backend performance, or guiding a mentee through their first pull request, I’m driven by the power of community and continuous learning. Let’s connect! I'm open to collaborations, mentorship, or building something impactful together. Reach out to me at kumaripayal7488@gmail.com or visit my profile on Topmate.io.