Decorator Design Pattern

According to wiki “decorator pattern is a design pattern that allows behavior to be added to an existing object dynamically.” But its lots more flexible than static inheritance. This pattern is also known as wrapper.

Consider different types of kindle packages. User may wants to know the price, descriptions of their custom package.  Say, a user like to have a kindle with a bag and an adapter. So how will you do it? This pattern solves this problem.

public class Kindle {

    public String getDescription() {
        return "kindle";
    }

    public double getWeight() {
        return 80.5;
    }

    public double getPrice() {
        return 199;
    }
}

Now create a decorator class to decorate kindle with bag and adapter.

public class KindleDecorator extends Kindle {
    Kindle kindle;

    public KindleDecorator(Kindle kindle) {
        this.kindle = kindle;
    }

    @Override
    public String getDescription() {
        return kindle.getDescription();
    }

    @Override
    public double getWeight() {
        return kindle.getWeight();
    }

    @Override
    public double getPrice() {
        return kindle.getPrice();
    }
}

Now the decorators are:

public class Bag extends KindleDecorator {
    public Bag(Kindle kindle) {
        super(kindle);
    }

    @Override
    public String getDescription() {
        return kindle.getDescription() + ", bag";
    }

    @Override
    public double getWeight() {
        return kindle.getWeight() + 20;
    }

    @Override
    public double getPrice() {
        return kindle.getPrice() + 40;
    }
}

public class Adapter extends KindleDecorator {
    public Adapter(Kindle kindle) {
        super(kindle);
    }

    @Override
    public String getDescription() {
        return kindle.getDescription() + ", adapter";
    }

    @Override
    public double getWeight() {
        return kindle.getWeight() + 50;
    }

    @Override
    public double getPrice() {
        return kindle.getPrice() + 30;
    }
}

Let see how it is used by the client:

Kindle kindleWithBagAndAdapter = new Adapter(new Bag(new Kindle()));
System.out.println("Package: Kindle With Bag And Adapter");
System.out.println("Description: " + kindleWithBagAndAdapter.getDescription());
System.out.println("Weight: " + kindleWithBagAndAdapter.getWeight() + " gm");
System.out.println("Price: $" + kindleWithBagAndAdapter.getPrice());

The output is:

Package: Kindle With Bag And Adapter
Description: kindle, bag, adapter
Weight: 150.5 gm
Price: $269.0

The pattern is different from Strategy in the way that Decorator Pattern is used to change the skin of object and Strategy Pattern is used to change the guts of Object.

Useful Links:

https://www.youtube.com/watch?v=j40kRwSm4VE
http://en.wikipedia.org/wiki/Decorator_pattern

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 )

Connecting to %s