0% found this document useful (0 votes)
56 views

Design Pattern Part 2

The document discusses three design patterns: Decorator, Memento, and Prototype. The Decorator pattern allows adding new behaviors to individual objects dynamically at runtime. The Memento pattern is used for undo/rollback and allows capturing and storing an object's internal state without violating encapsulation. The Prototype pattern involves specifying the types of objects to create using a prototypical instance and creating new objects by copying this prototype.

Uploaded by

Saad Hassan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Design Pattern Part 2

The document discusses three design patterns: Decorator, Memento, and Prototype. The Decorator pattern allows adding new behaviors to individual objects dynamically at runtime. The Memento pattern is used for undo/rollback and allows capturing and storing an object's internal state without violating encapsulation. The Prototype pattern involves specifying the types of objects to create using a prototypical instance and creating new objects by copying this prototype.

Uploaded by

Saad Hassan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Overview of Design Patterns

We will Discuss three design


patterns here named:

 Decoder
 Memento
 Prototype
Decorator Pattern

Type of Structural Design


Pattern
Concept of Decorating in Real World

After Decorating
PARTICIPANTS
DECORATOR PATTERN
APPROACH
Prototype Pattern

Type of Creational Design


Pattern
Prototype Pattern in Real Life

CLONING is an Example of Prototype Pattern


PROTOTYPE PATTERN
IN JAVA

PrototypeClient

PrototypeCreator

Prototype

Concrete Concrete
Prototype Prototype
How Prototype Pattern works in Real Life ?

clone

request

deliver

clone
Memento Pattern

Type of Behavioral Design


Pattern
Memento Pattern in Real Life

Making Undo easy is the intent


of Memento
PARTICIPANTS

• Memento
• Caretaker
• Originator
Stores internal
materials of the house
(like the store room of
home)
Example
Responsible for
storing &
maintaining
household good
(like a night guard)
Caretaker
Memento

Who stores in
house(like the
owner of the
house) Originator
Decorator
Design Pattern
INTENT

• The Decorator Pattern attaches


additional responsibilities to an object
dynamically. Decorators provide a
flexible alternative to Sub-classing for
extending functionality.
MOTIVATION
• Sometimes we want to add responsibilities to individual objects
not to an entire class.
• Inheriting responsibilities from another class attaches them to
every subclass instance statically. This is inflexible.
PROBLEM??
whenever a team member becomes a team lead,
we have to create a new object of team lead and the
previous object that points to that employee (team
member) maybe destroyed

Another case is when an employee can perform responsibilities of a team member


as well as those of a team lead or a manager can perform team leads
responsibilities. In that case you need to create two objects for the same employee
which is totally wrong.
DECORATOR PATTERN
APPROACH
Now, if we want to change responsibilities of an employee to
manager we just need a new Manager (Decorator) and assigning
that employee to it will solve our problem. Same is the case when a
team lead’s responsibilities are revoked, and some other member
becomes team lead, we just need to swap employee objects within
Team Member and Team Lead decorators
GRAPHICAL USER
INTERFACE TOOLKIT
STRUCTURE
EXAMPLE
APPLICABILITY
• To add responsibilities to individual objects
dynamically and transparently, that is, without
affecting other objects.
• For responsibilities that can be withdrawn.
•When extension by sub-classing is impractical.
Sometimes a large number of independent extensions
are possible and would produce an explosion of
subclasses to support every combination. Or a class
definition may be hidden or otherwise unavailable for
sub-classing.
PARTICIPANTS
• Component (VisualComponent)
defines the interface for objects that can have responsibilities
added to them dynamically

• ConcreteComponent (TextView)
defines an object to which additional responsibilities can be
attached.

• Decorator
maintains a reference to a Component object and defines
an interface that conforms to Component‘s interface.

• ConcreteDecorator (BorderDecorator,ScrollDecorator)
adds responsibilities to the component.
PARTICIPANTS
COLLABORATIONS

•Decorator forwards requests to its Component object. It


may optionally perform additional operations before and
after forwarding the request.
CONSEQUENCES

• More flexible than static inheritance.


• Avoids feature laden classes high up in
hierarchy.
• Lots of little objects that look alike. So it is
hard to learn and debug.
• A decorator and its components are not
identical. So checking object identification can
cause problems.
IMPLEMENTATION
• Several issues should be considered when applying the
Decorator pattern
• 1. Interface conformance: A decorator object’s interface
must conform to the interface of the component it
decorates.
• 2. Omitting the abstract Decorator class: If only one
responsibility is needed, don’t define abstract Decorator.
Merge Decorator’s responsibility into the Concrete
Decorator.
• 3. Keeping Component classes light weight: Component
class should be dedicated to defining an interface, no
other functions. Keep it light and simple. A complex
Component class might make Decorator too costly to use
in quantity.
• 4. Changing the skin of an object versus its guts:
Decorator classes should act as a layer of skin over an
object. If there’s a need to change the object’s guts, use
Strategy pattern.
Memento Pattern
Intent

 A Behavioral Pattern that can be used to promote


undo or rollback to object status.

• Without violating encapsulation, capture and


externalize an object’s internal state so that the
object can be returned to this state later
Motivation
 Often it is needed to backtrack when a particular path
proves unproductive.

 Examples: graph algorithms, text navigation etc.

 The exploration of some complex data structure is


done by many technical processes.
Applicability

When using
To Keep a direct interface to
snapshot of restore state will
object to violate design
restore later rule
Participants
 Memento
o stores internal state of the Originator object
o Inner class of originator & private.
o The Memento must have two interfaces

 Originator
o Creates a memento object
o Use the memento to save & restore

 Caretaker
o Responsible for keeping the memento
o The memento is black box to the caretaker,
and the caretaker must not operate on it

Internal behavior of code is unknown


Real life Scenerio I’m getting fatty!
Which pattern can
restore me to my
slim previous
state???

9
Yes!
Memento
Solution Is the
solution

*Diet_info class: originator


i. Dieter field
ii. No of diet day
iii. Weight of dieter
*inside memento class to save
previous state.
*save(): creates & return object
*Restore(): return previous state
Obiect stored
by caretaker

Diet info(Originator)

+Person name:string
+day no:int
Memento Diet info
+weight: int caretaker(caretak
-mementoPersonName: string er)
+DietInfo():void -mementoDayNumber: int
+setDayNumberAndWeight() -mementoWeight :int
+Object objMemento
:void
+saveState() :void
+save() :string +memento(): void
+restoreState :void
+Restore() :void
- Memento() :void
UML DIAGRAM
PROTOTYPE PATTERN
EXPLAINED !
OVERVIEW
Prototype is a creational pattern.
This pattern encapsulate the knowledge
about which classes a system users, but
they hide the details of how the instances
of these classes are created and put
together.
INTENT
Specify the kind of objects to create using
a prototypical instance, and create new objects
by copying this prototype.
Motivation

A prototype is a template of any object


before the actual object is constructed.
Prototype design pattern is used in
scenarios where application needs to create
a number of instances of a class, which has
almost same state or differs very little
Applicability
• When the classes to instance are
specified at run-time, for example, by
dynamic loading
• To avoid building a class hierarchy of
factories that parallels the class
hierarchy of products
• When instance of a class can have one
of only a few different combinations of
state.
UML DIAGRAM
PARTICIPANT
• Prototype
– Declares an interface for cloning itself
• Concrete Prototype
– Implements an operation for cloning itself
• Client
– Creates a new object by asking a prototype
to clone itself
CONSEQUENCES
• Adding and removing products at run-time
• Specifying new objects by varying values
• Specifying new objects by varying structure
• Reduced subclassing
• Configuring an application with classes
dynamically
Copy options
• Deep Copy (implementation in user
code), but in some languages exist
functions which do it
• Simple object clone (standard
function MemberwiseClone())
SIMPLE COPY
• C#

• Ruby
DEEP COPY
C# Ruby

You might also like