Decorator Design Pattern in java

     It is a design pattern allows the user to add a function at run time. But it doesn’t change the behavior of other objects in the same class. Basically, it is a structured design pattern.

Features:

  • ·       Reusable
  • ·       Flexible
  • ·       Extensible

 Components of decorator pattern:

  • ·       Component interface
  • ·       Concrete component
  • ·       Decorator
  • ·       Concrete decorator

 Component interface:

              A foundation code which has an abstract method. This is the basic for decorator and concrete components.

Concrete component:

              This is the class which develops the basic behaviour of the component interface.

Decorator component:

              It is an abstract class which refers the component object. It creates the decorator interface and component instance references.

Concrete decorator:

              It adds the functionality to decorator class.

Java Program to implement Decorator design pattern for Juice Shop:

Steps to create the code:

  • ·       First, create a component interface “Juice”.
  • ·       Create a  concrete component “Basic Juice” with two function implementation “getInfo()” and “getPrice()”.
  • ·       “JuiceDecorator” – it is an abstract decorator component.
  • ·       Last implementation is concrete decorator which is “IceDecorator” and “HoneyDecorator” with dynamic functional code addition.
  • ·       Finally, write the main function.It creates the objects for all classes and call the functions.

#Java Program to implement Decorator design pattern for Juice Shop

interface Juice {

    String getInfo();

    double getPrice();

}

class BasicJuice implements Juice {

    public String getInfo() {

        return "Basic Juice";

    }

    public double getPrice() {

        return 5.0;

    }

}

abstract class JuiceDecorator implements Juice {

    protected Juice decoratedJuice;

    public JuiceDecorator(Juice juice) {

        this.decoratedJuice = juice;

    }

    public String getInfo() {

        return decoratedJuice.getInfo();

    }

    public double getPrice() {

        return decoratedJuice.getPrice();

    }

}

class IceDecorator extends JuiceDecorator {

    public IceDecorator(Juice juice) {

        super(juice);

    }

    public String getInfo() {

        return super.getInfo() + ", with ICE";

    }

    public double getPrice() {

        return super.getPrice() + 1.5;

    }

}

 

class HoneyDecorator extends JuiceDecorator {

    public HoneyDecorator(Juice juice) {

        super(juice);

    }

    public String getInfo() {

        return super.getInfo() + ", with Honey";

    }

    public double getPrice() {

        return super.getPrice() + 0.5;

    }

}

public class JuiceApp {

    public static void main(String[] args) {

        Juice juice = new BasicJuice();

        System.out.println(juice.getInfo() + " $" + juice.getPrice());

        juice = new IceDecorator(juice);

        System.out.println(juice.getInfo() + " $" + juice.getPrice());

        juice = new HoneyDecorator(juice);

        System.out.println(juice.getInfo() + " $" + juice.getPrice());

    }

}

Output:

C:\raji\blog>javac JuiceApp.java

C:\raji\blog>java JuiceApp

Basic Juice $5.0

Basic Juice, with ICE $6.5

Basic Juice, with ICE, with Honey $7.0

This program creates a basic juice application with price displaying based on decorator design pattern.

No comments:

Post a Comment