Decorator Pattern
Design Patterns
Tharuka Dilani Siriwardena.
M.A.N.Shahitha Nazrin.
S.P Dimuthu Anuraj.
INTRODUCTION
DESIGN PATTERNS - DEFINITION
• A general reusable solution to a commonly occurring
problem in software design (Wikipedia).
• It is not a finished design.
• It is a description or template for how to solve a problem
that can be used in many different situations.
• Shows relationship and interactions between classes
and/or objects.
DESIGN PATTERNS - CLASSIFICATION
• Creational pattern
• Deal with object creation mechanism
• E.g. Abstract Factory, Singleton
• Structural pattern
• Ease the design by identifying a simple way to realize relationships
between entities
• E.g. Decorator, Proxy
• Behavioral pattern
• Deal with common communication between objects.
• E.g. Chain of Responsibility, Command
DESIGN PATTERNS – CLASSIFICATION
Structural Patterns
• 1. Decorator
• 2. Proxy
• 3. Bridge
• 4. Composite
• 5. Flyweight
• 6. Adapter
• 7. Facade
Creational Patterns
• 1. Prototype
• 2. Factory Method
• 3. Singleton
• 4. Abstract Factory
• 5. Builder
Behavioral Patterns
• 1. Strategy
• 2. State
• 3. TemplateMethod
• 4. Chain of
Responsibility
• 5. Command
• 6. Iterator
• 7. Mediator
• 8. Observer
• 9. Visitor
• 10. Interpreter
• 11. Memento
Decorator
Structural Design Pattern
CONCEPT OF DECORATING IN REAL
WORLD
Before Decorating
After Decorating
DEFINITION
• In object-oriented programming, the decorator
pattern is a design pattern that allows new/additional
behaviour to be added to an existing object dynamically.
Photo
+
Frame
+
Caption
RAD Family
DECORATOR PATTERN - EXPLAINED
• Adds functionality at Runtime.
• The object does not know it is being “decorated”.
• Three 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.
DECORATOR PATTERN - EXPLAINED
IMPLEMENTATION
• We're going to create a Shape interface and concrete classes
implementing the Shape interface. We will then create an
abstract decorator class ShapeDecorator implementing the
Shape interface and having Shape object as its instance
variable.
• RedShapeDecorator is concrete class implementing
ShapeDecorator.
• DecoratorPatternDemo, our demo class will use
RedShapeDecorator to decorate Shape objects.
DECORATOR PATTERN - UML
DIAGRAM
DEMO
Builder Pattern Decorator Pattern
• Graphics world (as illustrated).
• I/O namespace of .NET
• System.IO.Stream
• System.IO.BufferedStream
• System.IO.FileStream
• System.IO.MemoryStream
• Cross platform applications (Mobile/Desktop)
• Actual decorator classes in .NET 3.0
• System.Windows.Controls (Base Class)
• Border (Decorator)
• Viewbox (Decorator)
DECORATOR PATTERN – REAL WORLD
SAMPLE
• You have:
• An existing component class that may be unavailable for
subclassing.
• You want to:
• Attach additional state or behavior to an object dynamically.
• Make Changes to some objects of a class without affecting
others.
• Avoid subclassing because too many classes could result.
DECORATOR PATTERN – GUIDELINES(1)
• But consider using instead:
• The Adapter Pattern
• Sets up an interface between different classes
• The Composite Pattern
• Aggregates an object without also inheriting its interface.
• The Proxy Pattern
• Specifically controls access to objects.
• The Strategy Pattern
• Changes the original object rather than wrapping it.
DECORATOR PATTERN – GUIDELINES(2)
BUILDER VS DECORATOR
• Builder patterns makes it easy
to construct an object which
is extensible in independent
directions at construction
time
• Decorator pattern lets you
add extensions to
functionality to an object after
construction time
BUILDER VS DECORATOR
• Builder
Elements are must
This acts as a base on top other work
if this fails the object will be incomplete or inconsistent
• Decorator
Elements are optional
This is to add/attach more responsibility to existing
object
This is bonus or extra functionality is won’t affect the
basic behavior
Thank You !
ANY QUESTIONS?
Decorator Pattern

Decorator Pattern

  • 1.
    Decorator Pattern Design Patterns TharukaDilani Siriwardena. M.A.N.Shahitha Nazrin. S.P Dimuthu Anuraj.
  • 2.
  • 3.
    DESIGN PATTERNS -DEFINITION • A general reusable solution to a commonly occurring problem in software design (Wikipedia). • It is not a finished design. • It is a description or template for how to solve a problem that can be used in many different situations. • Shows relationship and interactions between classes and/or objects.
  • 4.
    DESIGN PATTERNS -CLASSIFICATION • Creational pattern • Deal with object creation mechanism • E.g. Abstract Factory, Singleton • Structural pattern • Ease the design by identifying a simple way to realize relationships between entities • E.g. Decorator, Proxy • Behavioral pattern • Deal with common communication between objects. • E.g. Chain of Responsibility, Command
  • 5.
    DESIGN PATTERNS –CLASSIFICATION Structural Patterns • 1. Decorator • 2. Proxy • 3. Bridge • 4. Composite • 5. Flyweight • 6. Adapter • 7. Facade Creational Patterns • 1. Prototype • 2. Factory Method • 3. Singleton • 4. Abstract Factory • 5. Builder Behavioral Patterns • 1. Strategy • 2. State • 3. TemplateMethod • 4. Chain of Responsibility • 5. Command • 6. Iterator • 7. Mediator • 8. Observer • 9. Visitor • 10. Interpreter • 11. Memento
  • 6.
  • 7.
    CONCEPT OF DECORATINGIN REAL WORLD Before Decorating After Decorating
  • 8.
    DEFINITION • In object-orientedprogramming, the decorator pattern is a design pattern that allows new/additional behaviour to be added to an existing object dynamically.
  • 9.
  • 10.
    • Adds functionalityat Runtime. • The object does not know it is being “decorated”. • Three 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. DECORATOR PATTERN - EXPLAINED
  • 11.
  • 12.
    • We're goingto create a Shape interface and concrete classes implementing the Shape interface. We will then create an abstract decorator class ShapeDecorator implementing the Shape interface and having Shape object as its instance variable. • RedShapeDecorator is concrete class implementing ShapeDecorator. • DecoratorPatternDemo, our demo class will use RedShapeDecorator to decorate Shape objects.
  • 13.
  • 14.
  • 15.
    • Graphics world(as illustrated). • I/O namespace of .NET • System.IO.Stream • System.IO.BufferedStream • System.IO.FileStream • System.IO.MemoryStream • Cross platform applications (Mobile/Desktop) • Actual decorator classes in .NET 3.0 • System.Windows.Controls (Base Class) • Border (Decorator) • Viewbox (Decorator) DECORATOR PATTERN – REAL WORLD SAMPLE
  • 16.
    • You have: •An existing component class that may be unavailable for subclassing. • You want to: • Attach additional state or behavior to an object dynamically. • Make Changes to some objects of a class without affecting others. • Avoid subclassing because too many classes could result. DECORATOR PATTERN – GUIDELINES(1)
  • 17.
    • But considerusing instead: • The Adapter Pattern • Sets up an interface between different classes • The Composite Pattern • Aggregates an object without also inheriting its interface. • The Proxy Pattern • Specifically controls access to objects. • The Strategy Pattern • Changes the original object rather than wrapping it. DECORATOR PATTERN – GUIDELINES(2)
  • 18.
    BUILDER VS DECORATOR •Builder patterns makes it easy to construct an object which is extensible in independent directions at construction time • Decorator pattern lets you add extensions to functionality to an object after construction time
  • 19.
    BUILDER VS DECORATOR •Builder Elements are must This acts as a base on top other work if this fails the object will be incomplete or inconsistent • Decorator Elements are optional This is to add/attach more responsibility to existing object This is bonus or extra functionality is won’t affect the basic behavior
  • 20.
    Thank You ! ANYQUESTIONS?