Friday, 20 December 2013

Decorator Design pattern - Real Time Example [Car]

🚗 Master Java Architecture & Design Patterns!

Enjoying this real-world breakdown? Subscribe to Ram N Java for clean coding techniques, object-oriented concepts, and step-by-step programming guides.

🔔 Subscribe to Ram N Java

What is the Decorator Design Pattern?

The Decorator Pattern is a structural design pattern that lets you attach additional responsibilities and capabilities to objects dynamically at runtime.

Rather than relying on deep, inflexible inheritance hierarchies, the Decorator Pattern leverages object composition. It wraps an existing object inside a decorator object that matches the same interface, extending its functionality seamlessly.

Real-World Analogy: Customizing a Vehicle

Think of a modern car factory assembly line:

  • You begin with a base car chassis consisting of the frame, doors, glass, and wheels—without an engine.
  • Customer A orders a car with a Petrol Engine.
  • Customer B orders a car with a Diesel Engine.

If you designed this system using strict inheritance, you would need dedicated subclasses for every single engine configuration. With the Decorator Pattern, the base car stays independent, and specialized engine decorators wrap it as needed!

How the Vehicle Decorator Works

1. The Base Component

A common interface defines the core contract for manufacturing or assembling a car:

public interface Car {
    void manufactureCar();
}

2. The Concrete Component

A standard car implementation provides the basic foundation:

public class BaseCar implements Car {
    @Override
    public void manufactureCar() {
        System.out.println("Base Car Assembled: Body, Wheels, Doors, Glass.");
    }
}

3. The Decorator Layer

The abstract decorator holds a reference to a wrapped Car object and forwards requests to it:

public abstract class CarDecorator implements Car {
    protected Car car;

    public CarDecorator(Car car) {
        this.car = car;
    }

    @Override
    public void manufactureCar() {
        car.manufactureCar();
    }
}

4. Adding Enhancements Dynamically

Specialized decorators add the engine behavior without altering the base vehicle code:

public class PetrolCarDecorator extends CarDecorator {
    public PetrolCarDecorator(Car car) {
        super(car);
    }

    @Override
    public void manufactureCar() {
        super.manufactureCar();
        System.out.println("Enhanced: Petrol Engine Installed.");
    }
}

Why Choose the Decorator Pattern?

  • Open/Closed Principle: Add new engines, accessories, or features without modifying existing classes.
  • Runtime Flexibility: Assemble customizations dynamically based on user needs.
  • Prevents Class Explosion: Eliminates the need to write endless subclass combinations.

Related Tutorials You Might Like

Broaden your software design knowledge with these related tutorials from the channel:


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

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


















See also:

  • Decorator Design pattern - Introduction
  • Decorator Design pattern - Real Time Example [Dosa]
  • Decorator Design pattern - Real Time Example [Ice Cream]
  • Decorator Design pattern - Real Time Example [Pizza]
  • 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