Design Pattern Part 2
Design Pattern Part 2
Decoder
Memento
Prototype
Decorator Pattern
After Decorating
PARTICIPANTS
DECORATOR PATTERN
APPROACH
Prototype Pattern
PrototypeClient
PrototypeCreator
Prototype
Concrete Concrete
Prototype Prototype
How Prototype Pattern works in Real Life ?
clone
request
deliver
clone
Memento Pattern
• 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
• 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
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
9
Yes!
Memento
Solution Is the
solution
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
• Ruby
DEEP COPY
C# Ruby