Thursday, April 29, 2010

Decorator Pattern

Structural Patterns - Decorator Pattern
The decorator pattern helps to add behavior or responsibilities to an object. This is also called “Wrapper”. Suppose we have some 6 objects and 2 of them need a special behavior, we can do this with the help of a decorator.
Java Design Patterns suggest that Decorators should be abstract classes and the concrete implementation should be derived from them.
The decorator pattern can be use wherever there is a need to add some functionality to the object or group of objects. Let’s take an example of a Christmas tree. There is a need to decorate a Christmas tree. Now we have many branches which need to be decorated in different ways.

Let’s have a look at the basic Decorator class.

Decorator.java
package structural.decorator; public abstract class Decorator {

  /*
* The method places each decorative item
* on the tree.
*/
public abstract void place(Branch branch);
}// End of class
This class has just one method place(). This method places different types of items on the branches of the tree.
The class ChristmasTree is very simple and has just one method which returns a branch.

ChristmasTree.java

package structural.decorator; public class ChristmasTree {

  private Branch branch; public Branch getBranch() {
return branch;
}
}// End of class
Now we can decorate the branches in three different ways, one is by putting coloured balls on them, by putting coloured ruffles on them and also by putting stars on them.
Let’s have a look at the implementation of these three different types of decorators.

BallDecorator.java

package structural.decorator; /**
* Decorates the branch of the tree with
* coloured balls.
*/
public class BallDecorator extends Decorator {

  // Default Constructor
public BallDecorator(ChristmasTree tree) {
Branch branch = tree.getBranch();
place(branch);
}
/*
* The method places each decorative item
* on the tree.
*/
public void place(Branch branch) {
branch.put("ball");
}
}// End of class
Similarly, we can make StarDecorator and RufflesDecorator. Now, we will see how to use the decorator. Its simple, we just are needed to pass the instance of ChristmasTree class to a decorator.
StarDecorator decorator = new StarDecorator(new ChristmasTree());
This way the decorator will be instantiated and a branch of the Christmas tree will be decorated.
This is a very abstract example and could not be implemented in terms of code fully. But, then, as I have said, I want you to understand the patterns rather than giving you concrete examples. Once the pattern is internalized, you can think of some good software examples yourself.
Decorators, Composites and Adapters
The decorator and adapter patterns are similar. Adapters also seem to decorate the classes. The intent of using adapter is to convert the interface of one or more classes to suit the interface of the client program. In case of decorator, the intent is to add behavior and functionality to some of the objects, not all the objects or adding different functionalities to each of the objects.

In case of composite objects, the client program treats the objects similarly, whether it is a simple or complex object (nodes).
The decorator pattern provides functionality to objects in a more flexible way rather than inheriting from them.
There is however disadvantage of using decorator. The disadvantage is that the code maintenance can be a problem as it provides the system with a lot of similar looking small objects (each decorator).

No comments:

Post a Comment