Quickly learning the Decorator Design Pattern with Java

Post to Twitter

Today we will quickly go over the code to implement the Decorator Design Pattern. We will focus only on the code by introducing a scenario and finding a better way to solve the given problem using the Decorator Pattern.


You own a fancy Pizza place and need to output the customer’s orders back to them. So you build a little class like such:

public class Pizza
{
	public String getOrder()
	{
		return "Pizza: Tomato Sauce, Cheese";
	}
}

To run this program you write this code:

public class RunPizza
{
	public static void main(String[] args)
	{
		Pizza pizza = new Pizza();
		System.out.println(pizza.getOrder());
	}
}

Result:

Pizza: Tomato Sauce, Cheese

That works just fine until a customer decides he/she doesn’t want just a plain pizza with the default of tomato sauce and cheese but instead wants to add pepperoni. You could modify the code to accommodate them like this:

public class Pizza
{
	public String getOrder()
	{
		return "Pizza: Tomato Sauce, Cheese, Pepperoni";
	}
}

The problem is when the next customer wants another topping or doesn’t even want pepperoni you need to modify that code again and again. A better way to handle this is with the decorator pattern by wrapping code around our Pizza class in order to extend it without having to modify it each time.

So how would this look? We keep the original Pizza class just as it is. Next we add an abstract class:

public abstract class PizzaToppingDecorator extends Pizza
{
	public abstract String getOrder();
}

Lets add a class now that extends that abstract class and makes an option for pepperoni.

public class PepperoniTopping extends PizzaToppingDecorator
{
	private Pizza pizza;

	public PepperoniTopping(Pizza p)
	{
		this.pizza = p;
	}

	@Override
	public String getOrder()
	{
		return this.pizza.getOrder() + ", Pepperoni";
	}
}

While we are at it, lets add a class to handle olives:

public class OliveTopping extends PizzaToppingDecorator
{
	private Pizza pizza;

	public OliveTopping(Pizza p)
	{
		this.pizza = p;
	}

	@Override
	public String getOrder()
	{
		return this.pizza.getOrder() + ", Olives";
	}
}

Time to test out the code:

public class RunPizza
{
	public static void main(String[] args)
	{
		Pizza pizza = new Pizza();
		System.out.println(pizza.getOrder());

		// Now the customer adds olives and pepperoni
		pizza = new PepperoniTopping(pizza);
		pizza = new OliveTopping(pizza);

		System.out.println(pizza.getOrder());
	}
}

Result:

Pizza: Tomato Sauce, Cheese
Pizza: Tomato Sauce, Cheese, Pepperoni, Olives

Voila the decorator pattern!

Post to Twitter

This entry was posted in Design Patterns, Java. Bookmark the permalink.

5 Responses to Quickly learning the Decorator Design Pattern with Java

  1. Pawan says:

    Very simple way to explain the concept.

  2. SureshKumar M S says:

    Fabulous way to explain decorator design pattern……

  3. SR says:

    Result showing is wrong it seems, it should be Pizza: Tomato Sauce, Cheese,Pepperoni and Pizza: Tomato Sauce, Cheese, Olives. Article is good.

  4. Assaduzzaman says:

    Great example ploicy

  5. Amir says:

    awesome, thanks a lot

Comments are closed.