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
8. 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.
10. • 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
12. • 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.
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 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)
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