Design patterns in C# - Decorator

Role

The role of the Decorator pattern is to provide a way of attaching new state and

behavior to an object dynamically. The object does not know it is being "decorated,"

which makes this a useful pattern for evolving systems. A key implementation

point in the Decorator pattern is that decorators both inherit the original class

and contain an instantiation of it.

Illustration

As its name suggests, the Decorator pattern takes an existing object and adds to it. As

an example, consider a photo that is displayed on a screen. There are many ways to

add to the photo, such as putting a border around it or specifying tags related to the

content. Such additions can be displayed on top of the photo.
 

The beauty of this pattern is that:

• The original object is unaware of any decorations.

• There is no one big feature-laden class with all the options in it.

• The decorations are independent of each other.

• The decorations can be composed together in a mix-and-match fashion.

Design

Now, we can specify the players in the Decorator pattern in a UML diagram, shown

in Figure 2-2. Because this is the first pattern we are describing in UML, we'll take it

slowly. (The UML that we need for patterns in this book is covered in Chapter 1 and

summarized in Table 1-1.) The essential players in this UML diagram are:

Component

An original class of objects that can have operations added or modified (there

may be more than one such class)

Operation

An operation in

IComponent objects that can be replaced (there may be several

operations)

IComponent

The interface that identifies the classes of objects that can be decorated

(

Component is one of these)

Decorator

A class that conforms to the

IComponent interface and adds state and/or behavior

(there may be more than one such class)

 

The center of the UML diagram is the

Decorator class. It includes two types of relationships

with the

IComponent interface:

Is-a

The is-a relationshipis shown by a dotted arrow from the

Decorator to

IComponent

, indicating that Decorator realizes the IComponent interface. The fact

that

Decorator inherits from IComponent means that Decorator objects can be

used wherever

IComponent objects are expected. The Component class is also in an

is-a relationshipwith

IComponent, and therefore the client can use Component and

Decorator

objects interchangeably—the heart of the Decorator pattern.

Has-a

The has-a relationshipis shown by an open diamond on the

Decorator, linked to

IComponent

. This indicates that the Decorator instantiates one or more

IComponent

objects and that decorated objects can outlive the originals. The

Decorator

uses the component attribute (of type IComponent) to invoke any

replacement

Operation it might wish to override. This is the way the Decorator

pattern achieves its objective.

The

addedBehavior operation and the addedState attribute in the Decorator class are

other optional ways of extending what is in the original

Component objects. We'll look

at some examples momentarily.



--
Alain Lompo
Excelta - Conseils et services informatiques
MCT
MCSD For Microsoft .Net
MVP Windows Systems Server / Biztalk Server
Certifié ITIL et Microsoft Biztalk Server

Commentaires

Posts les plus consultés de ce blog

Overview of Test Driven Development