The Decorator Pattern is a structural design pattern that allows you to dynamically attach additional responsibilities or behaviors to an object at runtime.
Rather than modifying original source code or using excessive inheritance, this pattern wraps an existing object with dedicated decorator classes using object composition.
Real-World Scenario: Customizing Cars Dynamically
Consider manufacturing a car. Initially, the core car assembly produces a base vehicle (doors, wheels, body, and glass) without a specific engine attached.
Depending on customer demand, that car needs to be equipped with either a Petrol Engine or a Diesel Engine. Creating separate fixed classes like PetrolBMWCar and DieselBMWCar creates tight coupling and code duplication.
By introducing a CarDecorator, we can take a base car and decorate it dynamically with the required engine type.
Step-by-Step Code Walkthrough
1. Component Interface
Define the core contract that both the basic car and decorators will share:
public interface Car {
void manufactureCar();
}
2. Concrete Component
Create the basic car implementation without any engine attached:
public class BMWCar implements Car {
@Override
public void manufactureCar() {
System.out.println("Manufacturing base BMW: Body, Doors, Wheels, Glass added.");
}
}
3. Abstract Decorator
The decorator implements Car and maintains a reference to the wrapped Car instance:
public abstract class CarDecorator implements Car {
protected Car car;
public CarDecorator(Car car) {
this.car = car;
}
@Override
public void manufactureCar() {
car.manufactureCar();
}
}
4. Concrete Decorators
Create specialized decorators that attach specific engine features to the car:
public class DieselCarDecorator extends CarDecorator {
public DieselCarDecorator(Car car) {
super(car);
}
@Override
public void manufactureCar() {
super.manufactureCar();
addDieselEngine();
}
private void addDieselEngine() {
System.out.println("Added: Diesel Engine installed.");
}
}
public class PetrolCarDecorator extends CarDecorator {
public PetrolCarDecorator(Car car) {
super(car);
}
@Override
public void manufactureCar() {
super.manufactureCar();
addPetrolEngine();
}
private void addPetrolEngine() {
System.out.println("Added: Petrol Engine installed.");
}
}
Key Advantages of This Approach
Runtime Customization: Behaviors can be added or swapped dynamically without rebuilding the core structure.
Open/Closed Principle: Add new engines or vehicle features without altering existing base car code.
Composition Over Inheritance: Prevents an explosion of subclasses for different feature combinations.
Related Tutorials You Might Like
Continue exploring core design patterns with these hands-on tutorials:
DieselCarDecorator added Diesel Engine to the Car : BMWCar [CARNAME=BMW, carBody=carbon fiber material, carDoor=4 car doors, carWheels=4 MRF wheels, carGlass=6 car glasses, engine=Diesel Engine]
PetrolEngineDecorator added Petrol Engine to the Car : BMWCar [CARNAME=BMW, carBody=carbon fiber material, carDoor=4 car doors, carWheels=4 MRF wheels, carGlass=6 car glasses, engine=Petrol Engine]
No comments:
Post a Comment