Decorator Design Pattern: Learn easy way?

Decorator Design Pattern
Share on facebook
Share on twitter
Share on linkedin
Share on email
Share on whatsapp
Share on pinterest
Share on print

Decorator Design Pattern intention is to decorate an object by adding behaviors without affecting other objects of the same class.  The Decorator Design Pattern allows users to add behaviors to a single object like a chain. This pattern comes under structural patterns ( Design Pattern).

In Decorator Design Pattern creates decorator classes using inheritance. Those classes use to wrap the main class by adding more functionality. Let’s take an example and clear this out.

Decorator Design Pattern Real World Example

Let’s take the manufacturing of the car industry as our example. Manufacturing car Factories make thousands of cars for a single model. But each single car object is easily personalized by adding different features to the car. As an example, cars can decorate by adding bumpers, extra lights, dashboard meters, and so on. But it’s not affecting other cars made by the same template.

This example creates SUV and Hatch Back cars with and without decorators. It clearly shows cars can decorate as a single object individually. Let’s move into the class diagram and its implementation.

Decorator Design Pattern
Decorator Design Pattern for Car Manufacturing

Decorator Design Pattern in Java

There are two types of cars SUV and Hatch Back. CarDecorator abstract class is the main Subject of this pattern and it’s created using implementing the car interface.

There can be many decorators as shown in the diagram. Those are inherited from the main CarDecorator class. This pattern helps to decorate cars without affecting other cars from the same model.

// Car model
interface Car{
   void manufacture();
}

// Basic SUV type car implemetaed
class SuvCar implements Car{
   @Override
   public void manufacture() {
      System.out.print("Manufacturing defalt SUV car ");
   }
}

// Basic HatchBack type car implemetaed
class HatchBackCar implements Car{
   @Override
   public void manufacture() {
      System.out.print("Manufacturing defalt HatchBack car ");
   }
}

// create abstract decorator
abstract class CarDecorator implements Car{
   protected Car car;
   public CarDecorator(Car car) {
      this.car=car;
   }
   @Override
   public void manufacture() {
      car.manufacture();
   }
}

// create concrete Bumper decorator
class BumperDecorator extends CarDecorator{
   public BumperDecorator(Car car) {
      super(car);
   }
   @Override
   public void manufacture() {
      car.manufacture();
      addBumper(car) ;
   }   
   public void addBumper(Car car){
      System.out.print("Adding Bumper to car ");
   }   
}

// create concrete TailLight decorator
class TailLightDecorator extends CarDecorator{
   public TailLightDecorator(Car car) {
      super(car);
   }
   @Override
   public void manufacture() {
      car.manufacture();
      addTailLight(car);
   }   
   public void addTailLight(Car car) {
      System.out.print("Adding Extra Tail Lights to car ");
   }
   
}

// create concrete DashBoardMeter decorator
class DashBoardMeterDecorator extends CarDecorator{

   public DashBoardMeterDecorator(Car car) {
      super(car);
   }
   @Override
   public void manufacture() {
      car.manufacture();
      addDashBoardMeter(car);
   }   
   public void addDashBoardMeter(Car car) {
      System.out.print("Adding Extra DashBoard Meters to car ");
   }
   
}

public class DeocoratorCarClient {
   public static void main(String[] args) {
      Car car1=new SuvCar();
      // Manufacturing Basic SUV type car
      car1.manufacture();
      System.out.println();
      System.out.println("---------------------------------------------");
      Car car2 = new BumperDecorator(new SuvCar());
      // Manufacturing SUV type car with only addition of Bumper
      car2.manufacture();
      System.out.println();
      System.out.println("---------------------------------------------");
      Car car3 = new TailLightDecorator(
                 new DashBoardMeterDecorator(
                 new BumperDecorator(new SuvCar())));
      // Manufacturing SUV type car with many additionals like Bumper, 
      // Tail Lights, Dashboard meters
      car3.manufacture();
      System.out.println();
      System.out.println("---------------------------------------------");
      Car car4 = new TailLightDecorator(
                 new DashBoardMeterDecorator(
                 new BumperDecorator(new HatchBackCar())));
      // Manufacturing HatchBack type car with many additionals like Bumper,
      // Tail Lights, Dashboard meters
      car4.manufacture();
      System.out.println();
      System.out.println("---------------------------------------------");
   }
}

Output

Manufacturing defalt SUV car 
---------------------------------------------
Manufacturing defalt SUV car Adding Bumper to car 
---------------------------------------------
Manufacturing defalt SUV car Adding Bumper to car Adding Extra DashBoard Meters to car Adding Extra Tail Lights to car 
---------------------------------------------
Manufacturing defalt HatchBack car Adding Bumper to car Adding Extra DashBoard Meters to car Adding Extra Tail Lights to car 

Advantages of Decorator Design Pattern

  • The main advantage is possible to change a single object without changing other objects from the same class
  • Easy to add a new decorator by extending the abstract decorator
  • Easy to remove decorator because those are loosely coupled.

Design Patterns Book

Design Patterns are one of the most famous solutions in programing and millions of people follow them to fix their tasks, design projects, and so on. Most of the patterns create based on basic OOP concepts (If your beginner then read our basic OOP, Encapsulation, Inheritance, Polymorphism, and Abstraction articles with great examples )

There are a lot of articles and design pattern books available with different examples to read. Other than our article if you are interested in this topic you could go through the below books as well.

BooksDescription

Head First Design Patterns: A Brain-Friendly Guide
My favorite and it’s more readable. There are good examples and they look good to beginners as well.

Design Patterns: Elements of Reusable Object-Oriented Software
There are 23 design patterns. showing how to select an appropriate pattern for your case.

Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software
There are a lot of simple examples showing how to use design patterns in the correct way. Those are based on SOLIC principles.
Design Patterns ebook online

There are a lot of design pattern ebooks on Amazon. Those design patterns are written with great examples in a different programming language. I included a few of them according to their best ranking.

Share on facebook
Share on twitter
Share on linkedin
Share on email
Share on whatsapp
Share on pinterest
Share on print

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Articles
You May Like
Subscribe to our Newsletter
Scroll to Top