Docsity

Decorator Pattern: A Flexible Approach to Object Customization - Prof. Atif M. Memon, Study notes of Programming Languages

Toggle sidebar
bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Decorator Pattern
Apr. 3, 2007
2
What is it?
• Suppose we have a program that uses eight objects, but
three of them need an additional feature.
• You could create a de rived class for each of these objects,
and in many cases this would be a perfectly acceptable
solution.
• However, if each of these three objects require different
modifications, this would mean creating three derived
classes. Further, if one of the classes has features of both
of the other classes, you begin to create a complexity that
is both confusing and unnecessary.
• The Decorator pattern provides us with a way to modify
the behavior of individual objects without having to c reate
a new derived class.
3
For Example
• Suppose we wanted to draw a special border around some
of the buttons in a toolbar.
• If we created a new de rived button class, this means that
all of the buttons in this new class would always have this
same new border, when this might not be our intent.
• Instead, we create a Decorator class that decorates the
buttons. Then we derive any number of specific Decorators
from the main Decorator class, each of which performs a
specific kind of decoration.
4
Example Decorator
• In order to dec orate a button, the Decorator has to be an
object derived from the visual environment, so it can
receive paint method calls and forward calls to other useful
graphic methods to the object that it is decorating.
• This is a case where object containment is favored over
object inherita nce.
• The decorator is a graphical object, but it contains the
object it is decorating.
• It may intercept some graphical method calls, perform
some additional computation and may pass them on to the
underlying object it is decorating.
pf3
pf4

Partial preview of the text

Download Decorator Pattern: A Flexible Approach to Object Customization - Prof. Atif M. Memon and more Study notes Programming Languages in PDF only on Docsity!

1

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2007

Decorator Pattern

Apr. 3, 2007

What is it?

three of them need an additional feature.Suppose we have a program that uses eight objects, but

solution.and in many cases this would be a perfectly acceptableYou could create a derived class for each of these objects,

However, if each of these three objects require

different

classes. Further, if one of the classes has features of modifications, this would mean creating three derived

both

is both confusing and unnecessary. of the other classes, you begin to create a complexity that

a new derived class.the behavior of individual objects without having to createThe Decorator pattern provides us with a way to modify

3

For Example

of the buttons in a toolbar.Suppose we wanted to draw a special border around some

same new border, when this might not be our intent.all of the buttons in this new class would always have thisIf we created a new derived button class, this means that

Instead, we create a Decorator class that

decorates

the

specific kind of decoration.from the main Decorator class, each of which performs abuttons. Then we derive any number of specific Decorators

Example Decorator

graphic methods to the object that it is decorating.receive paint method calls and forward calls to other usefulobject derived from the visual environment, so it canIn order to decorate a button, the Decorator has to be an

object inheritance.This is a case where object containment is favored over

object it is decorating.The decorator is a graphical object, but it contains the

underlying object it is decorating.some additional computation and may pass them on to theIt may intercept some graphical method calls, perform

5

Decorating a CoolButton

move your mouse over them.that highlight themselves with outline borders when youNetscape Navigator have a row of flat, unbordered buttonsWindows applications such as Internet Explorer and

buttons CoolButtons.Some Windows programmers call this toolbar a CoolBar and the

can obtain that behavior byThere is no analogous button behavior in the JFC, but we

decorating

a JButton.

over the button borders and erasing them.In this case, we decorate it by drawing plain gray lines

Strategy

JComponent class.We will derive our Decorator from the

method calls to the button it will contain.We will use its container properties to forward all

7

Strategy

to remove the button highlighting.class, and then draw gray lines around the borderdo is to draw the button as usual from the baseTo implement a CoolButton, all we really need to

13

Using the Two Decorators

Decorators, Adapters and Composites

you may have recognized.There is an essential similarity among these classes that

Adapters also seem to “decorate” an existing class.

program.more classes to one that is more convenient for a particularHowever, their function is to change the interface of one or

rather than to all of them.Decorators add methods to particular instances of classes,

single item is essentially a decorator.You could also imagine that a composite consisting of a

Once again, however, the intent is different.

15

Summary

to selected instances of the class.inheritance, since it can add these responsibilitiesway to add responsibilities to a class than by usingThe Decorator pattern provides a more flexible

subclasses high in the inheritance hierarchy.It allows you to customize a class without creating

maintenance headache.trying to maintain the code. This can be aobjects” that all look alike to the programmerDecorators can lead to a system with “lots of little