STRUCTURAL PATTERNS-
DECORATOR DESIGN PATTERN
Dr. S. Neelima
M.Tech Ph.D CSE
Priyadarshini Institute of Science and Technology
for Women, KHAMMAM
Intent
Decorator is a structural design pattern.
Attach additional responsibilities to an object dynamically. Decorators
provide a flexible alternative to sub classing for extending functionality.
Also Known as
Wrapper
Decorator Design Pattern- Dr. S. Neelima 2
Motivation
Imagine that you’re working on a notification library which lets other
programs notify their users about important events.
The initial version of the library was based on the Notifier class that had
only a few fields, a constructor and a single send method.
Decorator Design Pattern- Dr. S. Neelima 3
Motivation…..
Decorator Design Pattern- Dr. S. Neelima 4
Decorator Design Pattern- Dr. S. Neelima 5
The client code would need to wrap a basic notifier object into a set of
decorators that match the client’s preferences. The resulting objects will
be structured as a stack.
Decorator Design Pattern- Dr. S. Neelima 6
Motivation
2.
For example, suppose we have a TextView object that displays text
in a window. TextView has no scroll bars by default, because we might not
always need them. When we do, we can use a ScrollDecorator to add
them. Suppose we also want to add a thick black border around the
TextView. We can use a BorderDecorator to add this as well. We simply
compose the decorators with the TextView to produce the desired result.
Decorator Design Pattern- Dr. S. Neelima 7
Motivation
2. Solution :
Decorator Design Pattern- Dr. S. Neelima 8
Applicability
• Use the pattern to add responsibilities to individual objects
dynamically and transparently, that is, without affecting other objects.
• Use the pattern for responsibilities that can be withdrawn.
• Use the pattern when it’s awkward or not possible to extend an object’s
behavior using inheritance.
Decorator Design Pattern- Dr. S. Neelima 9
Structure
Decorator Design Pattern- Dr. S. Neelima 10
Participants
• Component (VisualComponent)
• Concrete Component (TextView)
• Decorator
• ConcreteDecorator
Decorator Design Pattern- Dr. S. Neelima 11
Collaborations
Decorator forwards requests to its Component object. It may optionally
perform additional operations before and after forwarding the request.
Consequences
Pros
• You can extend an object’s behavior without making a new subclass.
• You can add or remove responsibilities from an object at runtime.
• You can combine several behaviors by wrapping an object into multiple
decorators.
• Single Responsibility Principle. You can divide a monolithic class that
implements many possible variants of behavior into several smaller
classes.
Cons
• It’s hard to remove a specific wrapper from the wrappers stack.
• It’s hard to implement a decorator in such a way that its behavior
doesn’t depend on the order in the decorators stack.
• The initial configuration code of layers might look pretty ugly.
Decorator Design Pattern- Dr. S. Neelima 12
Implement
• Make sure your business domain can be represented as a primary
component with multiple optional layers over it.
• Figure out what methods are common to both the primary component
and the optional layers. Create a component interface and declare those
methods there.
• Create a concrete component class and define the base behavior in it.
• Create a base decorator class. It should have a field for storing a
reference to a wrapped object. The field should be declared with the
component interface type to allow linking to concrete components as
well as decorators. The base decorator must delegate all work to the
wrapped object.
Decorator Design Pattern- Dr. S. Neelima 13
Implement ….
• Make sure all classes implement the component interface.
• Create concrete decorators by extending them from the base decorator.
A concrete decorator must execute its behavior before or after the call to
the parent method (which always delegates to the wrapped object).
• The client code must be responsible for creating decorators and
composing them in the way the client needs.
Decorator Design Pattern- Dr. S. Neelima 14
Known Uses
Decorator Design Pattern- Dr. S. Neelima 15
• InterViews ,ET++ and the ObjectWorksSmalltalk class library.
• More exotic applications of Decorator are the DebuggingGlyph from
InterViews and the PassivityWrapper from ParcPlace Smalltalk.
Related Patterns
• Adapter changes the interface of an existing object,
while Decorator enhances an object without changing its interface.
• Composite and Decorator have similar structure diagrams since both
rely on recursive composition to organize an open-ended number of
objects.
• Decorator lets you change the skin of an object, while Strategy lets
you change the guts.
Decorator Design Pattern- Dr. S. Neelima 16
Decorator Design Pattern- Dr. S. Neelima 17

Decorator design pattern

  • 1.
    STRUCTURAL PATTERNS- DECORATOR DESIGNPATTERN Dr. S. Neelima M.Tech Ph.D CSE Priyadarshini Institute of Science and Technology for Women, KHAMMAM
  • 2.
    Intent Decorator is astructural design pattern. Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub classing for extending functionality. Also Known as Wrapper Decorator Design Pattern- Dr. S. Neelima 2
  • 3.
    Motivation Imagine that you’reworking on a notification library which lets other programs notify their users about important events. The initial version of the library was based on the Notifier class that had only a few fields, a constructor and a single send method. Decorator Design Pattern- Dr. S. Neelima 3
  • 4.
  • 5.
    Decorator Design Pattern-Dr. S. Neelima 5
  • 6.
    The client codewould need to wrap a basic notifier object into a set of decorators that match the client’s preferences. The resulting objects will be structured as a stack. Decorator Design Pattern- Dr. S. Neelima 6
  • 7.
    Motivation 2. For example, supposewe have a TextView object that displays text in a window. TextView has no scroll bars by default, because we might not always need them. When we do, we can use a ScrollDecorator to add them. Suppose we also want to add a thick black border around the TextView. We can use a BorderDecorator to add this as well. We simply compose the decorators with the TextView to produce the desired result. Decorator Design Pattern- Dr. S. Neelima 7
  • 8.
    Motivation 2. Solution : DecoratorDesign Pattern- Dr. S. Neelima 8
  • 9.
    Applicability • Use thepattern to add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects. • Use the pattern for responsibilities that can be withdrawn. • Use the pattern when it’s awkward or not possible to extend an object’s behavior using inheritance. Decorator Design Pattern- Dr. S. Neelima 9
  • 10.
  • 11.
    Participants • Component (VisualComponent) •Concrete Component (TextView) • Decorator • ConcreteDecorator Decorator Design Pattern- Dr. S. Neelima 11 Collaborations Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request.
  • 12.
    Consequences Pros • You canextend an object’s behavior without making a new subclass. • You can add or remove responsibilities from an object at runtime. • You can combine several behaviors by wrapping an object into multiple decorators. • Single Responsibility Principle. You can divide a monolithic class that implements many possible variants of behavior into several smaller classes. Cons • It’s hard to remove a specific wrapper from the wrappers stack. • It’s hard to implement a decorator in such a way that its behavior doesn’t depend on the order in the decorators stack. • The initial configuration code of layers might look pretty ugly. Decorator Design Pattern- Dr. S. Neelima 12
  • 13.
    Implement • Make sureyour business domain can be represented as a primary component with multiple optional layers over it. • Figure out what methods are common to both the primary component and the optional layers. Create a component interface and declare those methods there. • Create a concrete component class and define the base behavior in it. • Create a base decorator class. It should have a field for storing a reference to a wrapped object. The field should be declared with the component interface type to allow linking to concrete components as well as decorators. The base decorator must delegate all work to the wrapped object. Decorator Design Pattern- Dr. S. Neelima 13
  • 14.
    Implement …. • Makesure all classes implement the component interface. • Create concrete decorators by extending them from the base decorator. A concrete decorator must execute its behavior before or after the call to the parent method (which always delegates to the wrapped object). • The client code must be responsible for creating decorators and composing them in the way the client needs. Decorator Design Pattern- Dr. S. Neelima 14
  • 15.
    Known Uses Decorator DesignPattern- Dr. S. Neelima 15 • InterViews ,ET++ and the ObjectWorksSmalltalk class library. • More exotic applications of Decorator are the DebuggingGlyph from InterViews and the PassivityWrapper from ParcPlace Smalltalk.
  • 16.
    Related Patterns • Adapterchanges the interface of an existing object, while Decorator enhances an object without changing its interface. • Composite and Decorator have similar structure diagrams since both rely on recursive composition to organize an open-ended number of objects. • Decorator lets you change the skin of an object, while Strategy lets you change the guts. Decorator Design Pattern- Dr. S. Neelima 16
  • 17.
    Decorator Design Pattern-Dr. S. Neelima 17