Decorator Pattern – Design Pattern – Composition and Delegation

Design Principle

Classes should be open
for extension, but closed for
modification.

The Decorator Pattern attaches additional
responsibilities to an object dynamically.
Decorators provide a flexible alternative to
subclassing for extending functionality.

Okay, here’s what we know so far.. .

  • Decorators have the same supertype as the objects they decorate.
  • You can use one or more decorators to wrap an object.
  • Given that the decorator has the same supertype as the object it decorates, we can pass
    around a decorated object in place of the original (wrapped) object.
  • The decorator adds its own behavior either before and/or after delegating to the object it
    decorates to do the rest of the job.
  • Objects can be decorated at any time, so we can decorate objects dynamically at runtime
    with as many decorators as we like.

Let’s understand it from code

We have 4 packages…
1. com.designpattern.decorator
2. com.designpattern.coffee
3. com.designpattern.condiments
4. com.designpattern.starbuzz

com.designpattern.decorator

Bevarage.java

package com.designpattern.decorator;

/**
 *
 * @author taher.tinwala
 */
public abstract class Beverage {

    public String description = "Unknown Beverage";

    public String getDescription() {
        return description;
    }

    public abstract double cost();
}

CondimentDecorator.java

package com.designpattern.decorator;

/**
 *
 * @author taher.tinwala
 */
public abstract class CondimentDecorator extends Beverage {

    @Override
    public abstract String getDescription();
}

com.designpattern.coffee

Espresso.java

package com.designpattern.coffee;
import com.designpattern.decorator.Beverage;

/**
 *
 * @author taher.tinwala
 */
public class Espresso extends Beverage{

    public Espresso() {
        description = "Espresso";
    }

    @Override
    public double cost() {
        return 1.99;
    }
}

HouseBlend.java

package com.designpattern.coffee;
import com.designpattern.decorator.Beverage;

/**
 *
 * @author taher.tinwala
 */
public class HouseBlend extends Beverage{

    public HouseBlend() {
        description = "HouseBlend";
    }

    @Override
    public double cost() {
        return 0.89;
    }
}

com.designpattern.condiments

Mocha.java

package com.designpattern.condiments;

import com.designpattern.decorator.Beverage;
import com.designpattern.decorator.CondimentDecorator;

/**
 *
 * @author taher.tinwala
 */
public class Mocha extends CondimentDecorator {

    Beverage beverage;

    public Mocha(Beverage beverage) {
        this.beverage = beverage;
    }

    @Override
    public String getDescription() {
        return beverage.getDescription() + ", Mocha";

    }

    @Override
    public double cost() {
        return .20 + beverage.cost();
    }
}

Whip.java

package com.designpattern.condiments;

import com.designpattern.decorator.Beverage;
import com.designpattern.decorator.CondimentDecorator;

/**
 *
 * @author taher.tinwala
 */
public class Whip extends CondimentDecorator {

    Beverage beverage;

    public Whip(Beverage beverage) {
        this.beverage = beverage;
    }

    @Override
    public String getDescription() {
        return beverage.getDescription() + ", Whip";

    }

    @Override
    public double cost() {
        return .10 + beverage.cost();
    }
}

com.designpattern.starbuzz

StarBuzz.java

package com.designpattern.starbuzz;

import com.designpattern.coffee.Espresso;
import com.designpattern.coffee.HouseBlend;
import com.designpattern.condiments.Mocha;
import com.designpattern.condiments.Whip;
import com.designpattern.decorator.Beverage;

/**
 *
 * @author taher.tinwala
 */
public class StarBuzz {

    public static void main(String args[]) {
        Beverage beverage = new Espresso();
        System.out.println(beverage.getDescription()
                + " $" + beverage.cost());
        Beverage beverage2 = new HouseBlend();
        beverage2 = new Mocha(beverage2);
        beverage2 = new Mocha(beverage2);
        beverage2 = new Whip(beverage2);
        System.out.println(beverage2.getDescription()
                + " $" + beverage2.cost());
        Beverage beverage3 = new HouseBlend();
        beverage3 = new Mocha(beverage3);
        beverage3 = new Whip(beverage3);
        System.out.println(beverage3.getDescription()
                + " $" + beverage3.cost());
    }
}
Advertisements
This entry was posted in Core JAVA, Design Pattern, Just 4 Knowledge. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s