www.developer.com/tech/article.php/629081
|
July 31, 2000
DecoratorAs mentioned at the beginning of this article, the decorator pattern is structurally similar to the Adapter pattern but differs greatly in the intent.Suppose you are responsible for maintaining software of a security system for controlling physical access to a building. Its basic architecture is that a card reader or other data-entry device captures some identifying information and passes that information to an object that controls a door. If the object that controls the door is satisfied with the information, it unlocks the door. Here is a collaboration diagram showing that: Figure 4. Basic Physical Access Control Suppose you need to integrate this access control mechanism with a surveillance system. A surveillance system typically has more cameras connected to it than it has TV monitors. Most of the TV monitors will cycle through the images of different cameras. They show a picture from each camera for a few seconds and then move on to the next camera for which the monitor is responsible. There are rules about how the surveillance system is supposed to be set up to ensure its effectiveness. For this discussion, the relevant rules are:
Figure 5. Security System Classes There are three different kinds of doors installed and two different kinds of surveillance monitors in use. You could resolve the situation by writing two subclasses of each of the door controller classes, but you would rather not have to write six classes. Instead, you use the Decorator pattern, which solves the problem by delegation rather than inheritance. What you do is write two new classes called DoorControllerA and DoorControllerB. These classes both implement the DoorControllerIF interface: Figure 6. Door Controller Classes The new class, AbstractDoorControllerWrapper, is an abstract class that implements all of the methods of the DoorController interface with implementations that simply call the corresponding method of another object that implements the DoorController interface. The DoorControllerA and DoorControllerB, are concrete wrapper classes. They extend the behavior of the requestOpen implementation that they inherit to also ask a surveillance monitor to display its view of that doorway. Here is a collaboration diagram showing this: Figure 7. Door Surveillance Collaboration This approach allows doorways viewed by multiple cameras to be handled by simply putting multiple wrappers in front of the DoorControllerIF object. The following class diagram shows the general structure of the Decorator pattern: Figure 8. Decorator Pattern Below are descriptions of the roles that classes play in the Decorator pattern:
Here is an implementation of the DoorControllerIF interface: Here is the AbstractDoorControllerWrapper class that provides default implementations to its subclasses for the methods declared by the DoorControllerIF interface: Finally, here is a subclass of the AbstractDoorControllerWrapper class that extends the default behavior by asking a monitor to display the image from a named camera: Both the Adapter and Decorator patterns involve objects that are wrappers for other objects. The basic intent of the Adapter pattern is to adapt an object to an interface that we do not implement. The basic intent of the Decorator pattern is to extend the behavior of objects. This difference in intent is sometimes the clearest way to distinguish between the two.About the AuthorMark Grand is the author of a series of books titled "Patterns in Java." He is a consultant who specializes in object-oriented design and Java. He is currently working on a framework to create integrated enterprise applications. |