Decorator Pattern
Author: Deron Eriksson
Description: This Java tutorial describes the decorator pattern, a structural design pattern.
Tutorial created using: Windows Vista || JDK 1.6.0_11 || Eclipse JEE Ganymede SR1 (Eclipse 3.4.1)


Page:    1 2 >

The decorator pattern is a structural design pattern. Whereas inheritance adds functionality to classes, the decorator pattern adds functionality to objects by wrapping objects in other objects. Each time additional functionality is required, the object is wrapped in another object. JavaSW I/O streams are a well-known example of the decorator pattern.

For the decorator pattern, we require a class that serves as the base object that we add functionality to. This is a Concrete Component, and it implements a Component interface. The Component interface declares the common operations that are to be performed by the concrete component and all the decorators that wrap the concrete component object. A Decorator is an abstract class that implements the Component interface and contains a reference to a Component. Concrete Decorators are classes that extend Decorator.

We can illustrate the decorator pattern with an example. We'll start by creating an Animal interface. The Animal interface is our component interface.

Animal.java

package com.cakes;

public interface Animal {

	public void describe();

}

LivingAnimal implements Animal and is our concrete component. Its describe() method displays a message indicating that it is an animal.

LivingAnimal.java

package com.cakes;

public class LivingAnimal implements Animal {

	@Override
	public void describe() {
		System.out.println("\nI am an animal.");
	}

}

AnimalDecorator is our decorator abstract class. It implements Animal but since it is an abstract class, it does not have to implement describe(). Its constructor sets its Animal reference field.

AnimalDecorator.java

package com.cakes;

public abstract class AnimalDecorator implements Animal {

	Animal animal;

	public AnimalDecorator(Animal animal) {
		this.animal = animal;
	}

}

LegDecorator is a concrete decorator. Its constructor passes an Animal reference to AnimalDecorator's constructor. Its describe() method call's the Animal reference's describe() method and then outputs an additional message. It then calls its dance() method, showing that additional functionality can be added by the concrete decorator.

LegDecorator.java

package com.cakes;

public class LegDecorator extends AnimalDecorator {

	public LegDecorator(Animal animal) {
		super(animal);
	}

	@Override
	public void describe() {
		animal.describe();
		System.out.println("I have legs.");
		dance();
	}

	public void dance() {
		System.out.println("I can dance.");
	}

}

WingDecorator is a concrete decorator very similar to LegDecorator.

WingDecorator.java

package com.cakes;

public class WingDecorator extends AnimalDecorator {

	public WingDecorator(Animal animal) {
		super(animal);
	}

	@Override
	public void describe() {
		animal.describe();
		System.out.println("I have wings.");
		fly();
	}

	public void fly() {
		System.out.println("I can fly.");
	}

}

GrowlDecorator is another concrete decorator.

GrowlDecorator.java

package com.cakes;

public class GrowlDecorator extends AnimalDecorator {

	public GrowlDecorator(Animal animal) {
		super(animal);
	}

	@Override
	public void describe() {
		animal.describe();
		growl();
	}

	public void growl() {
		System.out.println("Grrrrr.");
	}

}

(Continued on page 2)

Page:    1 2 >


Related Tutorials: