Presents your JAVA E-NEWSLETTER for September 26, 2002 <-------------------------------------------> ADD FUNCTIONALITY WITH THE DECORATOR PATTERN You're probably already using the Decorator pattern, but as with all patterns, it's important to know why you're using it and to learn how to communicate about it with other developers. The main force governing usage of the Decorator pattern--also known as the Wrapper pattern--is a need to add functionality to a class without modifying its code or disturbing its inheritance. While you may want the class to have more functionality, using the Decorator pattern means that your improved functionality version doesn't have to reuse the existing functionality by extending the class. Given an interface named Action with two methods on it, act1() and act2(), and a concrete version of this interface named ConcreteAction, the Decorator would be a class that implements Action and whose constructor takes an Action (usually ConcreteAction). So the code would look like this: public class ActionDecorator implements Action { private Action action; public ActionDecorator(Action action) { this.action = action; } public void act1() { action.act1(); } public void act2() { // do nothing } } Using the Decorator results in fewer classes than inheritance, so coding is simpler; however, it usually results in more objects, which can make debugging harder, especially since the added flexibility can introduce new areas for errors. ----------------------------------------