


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



1
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
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
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
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
7
13
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