Tuesday, 2 September 2014

Decorator Design Pattern Implementation

Decorator Design Pattern

As we know that Decorator design pattern belongs to Structural pattern. This allows to add new functionality to an existing object either statically or dynamically, without affecting the behavior of other objects from the same class.

As per GOF "Allows for the dynamic wrapping of objects in order to modify their existing responsibilities and behaviors."

In other words, decorator design pattern allows us to construct a wrapper around the object by enhancing its functionality. The basic thing is we are wrapping the original object through decorated object. 

The below elements are the participants of the Decorator Design pattern:
  • Component – This defines the interface for objects that can have responsibilties added either statically or dynamically.
  • Concrete Component - This is simply an implementation of this interface.
  • Decorator - This has a reference to a Component, and also implements the  Component interface.
  • Concrete Decorator - This just adds extra responsibilities to the original Component.
Example :- In the below example Car is the Component, HatchbackCar is the Concrete Component, CarDecor is the Decorator and PremiumHatchbackCar & LuxurySportsCar is the Concrete Decorator.

1. Car.java

package com.gaurav.designpattern.decorator;

/**
 * @author gaurav
 *
 */
public interface Car {
public void addedFeatures();
}

2. HatchbackCar.java

package com.gaurav.designpattern.decorator;

/**
 * @author gaurav
 *
 */
public class HatchbackCar implements Car{

@Override
public void addedFeatures() {
System.out.println("This is a Basic HatchBack Car, No Premium facilities added.");
}

}

3. CarDecor.java

package com.gaurav.designpattern.decorator;

/**
 * @author gaurav
 *
 */
public class CarDecor implements Car{
protected Car car;
public CarDecor(Car c){
this.car = c;
}
@Override
public void addedFeatures() {
this.car.addedFeatures();
}
 
}

4. PremiumHatchbackCar.java

package com.gaurav.designpattern.decorator;

/**
 * @author gaurav
 *
 */
public class PremiumHatchbackCar extends CarDecor {

public PremiumHatchbackCar(Car c) {
super(c);
}

@Override
public void addedFeatures() {
car.addedFeatures();
System.out
.println("Few premium facilities are available in Hyundai Elite i20 like Power Windows-Front, Low Fuel Warning Light, Fabric Upholstery e.t.c");
}
}

5. LuxurySportsCar.java

package com.gaurav.designpattern.decorator;

/**
 * @author gaurav
 * 
 */
public class LuxurySportsCar extends CarDecor {

public LuxurySportsCar(Car c) {
super(c);
}

@Override
public void addedFeatures() {
car.addedFeatures();
System.out
.println("Luxury facilities are available in Mercedes-Benz A Class like Multi-function steering wheel, Bluetooth® telephony, Sunroof, Retrofitted anti-glare film e.t.c");
}
}

6. DecoratorDesignPatternDemo.java

package com.gaurav.designpattern.decorator;

/**
 * @author gaurav
 * 
 */
public class DecoratorDesignPatternDemo {
public static void main(String args[]) {
Car premiumHatchbackCar = new PremiumHatchbackCar(new HatchbackCar());
premiumHatchbackCar.addedFeatures();
System.out.println("*************************************************");

Car luxurySportsCar = new PremiumHatchbackCar(new LuxurySportsCar(
new HatchbackCar()));
luxurySportsCar.addedFeatures();
System.out.println("*************************************************");

}
}

Result:-

This is a Basic HatchBack Car, No Premium facilities added.
Few premium facilities are available in Hyundai Elite i20 like Power Windows-Front, Low Fuel Warning Light, Fabric Upholstery e.t.c
*************************************************
This is a Basic HatchBack Car, No Premium facilities added.
Luxury facilities are available in Mercedes-Benz A Class like Multi-function steering wheel, Bluetooth® telephony, Sunroof, Retrofitted anti-glare film e.t.c
Few premium facilities are available in Hyundai Elite i20 like Power Windows-Front, Low Fuel Warning Light, Fabric Upholstery e.t.c
*************************************************





No comments:

Post a Comment