Decorator Design Pattern in Java

In this module, we are going to discuss Decorator Design Pattern and it’s implementation in Java. Decorator Pattern is one of a Structural Design Pattern. Structural Design Pattern is one of the Core Java Design Patterns.

Properties of Decorator Pattern in Java

  • The main aim of the Decorator Pattern is to add Extra Functionalities and change the behavior of the object without affecting the other objects of the same class.
  • we can add functionalities to the object only at the run time but not at the compile time i.e during the creation of an object.
  • The added Functionalities does not affect the original class in any fashion.
  • Decorator Pattern uses a wrapper class for an object to extend its behavior.
  • We can use abstract classes or interface to implement decorator patterns.

The following code implements the Decorator Pattern in Java.

import java.io.*;
import java.util.*;
interface Benz_Car {
  public String name();                     //Interface for Benz Car
  public int price();
}
class BaseCar implements Benz_Car {

  @Override
  public String name() {
    return "BENZ";
  }                                              //This is Base Class on Which Decorators will be added
  @Override
  public int price(){
      return 5000000;
  }

}
abstract class Car_Decorator implements Benz_Car {

  protected Benz_Car Categories;

  public Car_Decorator(Benz_Car Categories) {
    this.Categories = Categories;
  }
                                               //This is a Decorator Class which implements interface Benz Car
  public String name() {
    return Categories.name();
  }
   public int price(){
      return Categories.price();
  }

}
class Luxery_Car extends Car_Decorator {                  //Inhertiance class of Car_Decorator

  public Luxery_Car(Benz_Car Categories) {
    super(Categories);
  }

  public String name() {
    return Categories.name() + Luxery_name();
  }
  public int price(){       
      return Categories.price()+ Luxery_price();
  }
                                              // This is a class that implements abstract class Decorator
  private String Luxery_name() {
    return "  A-Class";
  }
  private int Luxery_price()
  {
      return 6500000;
  }
}

class Sports_Car extends Car_Decorator {                  //Inheritance class of Car_Decorator

  public Sports_Car(Benz_Car Categories) {
    super(Categories);
  }

  public String name() {
    return Categories.name() + Sports_name();
  }
  public int price(){
      return Categories.price()+ Sports_price();
  }
                                           //This is a class that implements abstract class Decorator
  private String Sports_name() {
    return "  300 SLR";
  }
  private int Sports_price()
  {
      return 100000000;
  }
}

public class TestCases {

  public static void main(String args[])throws IOException {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int n=Integer.parseInt(br.readLine());
    System.out.println("1.Base Car"+"\t2.Luxery Car"+"\t3.Sports Car");
    switch(n)
    {
        case 1:
            BaseCar bs=new BaseCar();
            System.out.println(bs.name());
            System.out.println(bs.price());
            break;
        case 2:
            
            Benz_Car brz=new Luxery_Car(( Benz_Car)new BaseCar()); //Object of implementing Luxerycar with basecar         
            System.out.println(brz.name());
            System.out.println(brz.price());
            break;
        case 3:
             Benz_Car br1=new Sports_Car(( Benz_Car)new BaseCar());
            System.out.println(br1.name());
            System.out.println(br1.price());
            break;
        default:
        System.out.println("Wrong Choice");
    }
  }
}

Output :

1.Base Car     2.Luxery Car    3.Sports Car
1
BENZ
5000000

1.Base Car     2.Luxery Car    3.Sports Car
2
BENZ A-Class
11500000

1.Base Car     2.Luxery Car    3.Sports Car
3
BENZ 300 SLR
105000000

Explanation:

Here the Output is Base Car which can be invoked by using normal object instantiation. Later, we can see  Luxury Cars and Sports Cars are Decorators which we implemented on Base Car. Thus resulting in a result based on the object we would like to add properties.

Leave a Reply

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