0% found this document useful (0 votes)
9 views22 pages

Unit 4 3

The Memento pattern is used to restore an object to its previous state. It allows capturing and externalizing an object's internal state so it can be restored later, while preserving encapsulation. A caretaker manages mementos and requests them from the originator to restore its state without violating encapsulation. The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified automatically. It is commonly used in social media platforms where users follow subjects and are notified of their updates. The pattern involves a subject, concrete subject, observer interface, and concrete observers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views22 pages

Unit 4 3

The Memento pattern is used to restore an object to its previous state. It allows capturing and externalizing an object's internal state so it can be restored later, while preserving encapsulation. A caretaker manages mementos and requests them from the originator to restore its state without violating encapsulation. The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified automatically. It is commonly used in social media platforms where users follow subjects and are notified of their updates. The pattern involves a subject, concrete subject, observer interface, and concrete observers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Memento Pattern

Also Known As: Token


• Memento design pattern is behavioral pattern
and one of 23 design patterns discussed by
Gang of Four. Memento pattern is used to
restore state of an object to a previous state.
It is also known as snapshot pattern.
• Motivation :Sometimes it's necessary to
record the internal state of an object. This is
required when implementing checkpoints and
undo mechanisms that let users back out of
tentative operations or recover from errors.
Example
Structure & Participants
Applicability: Use the Memento pattern when
• A snapshot of (some portion of) an object's
state must be saved so that it can be restored
to that state later, and
• A direct interface to obtaining the state would
expose implementation details and break the
object's encapsulation
• Collaborations A caretaker requests a
memento from an originator, holds it for a
time, and passes its back to the originator, as
the following interaction diagram illustrates:
Related Patterns
• Command: Commands can use mementos to
maintain state for undoable operations
• Iterator: Mementos can be used for iteration
as described earlier
observer
• Intent: Define a one-to-many dependency
between objects so that when one object
changes state, all its dependents are notified
and updated automatically.
• Also Known As: Dependents, Publish-
Subscribe
A real world example of observer pattern can be any social
media platform such as Facebook or twitter. When a person
updates his status – all his followers gets the notification. A
follower can follow or unfollow another person at any point
of time. Once unfollowed, person will not get the
notifications from subject in future.
Example
Structure
Applicability: Use the Observer pattern in any
of the following situations:
• When an abstraction has two aspects, one
dependent on the other. Encapsulating these
aspects in separate objects lets you vary and
reuse them independently
• When a change to one object requires
changing others, and you don't know how
many objects need to be changed
Collaborations
• ConcreteSubject notifies its observers
whenever a change occurs that could make its
observers' state inconsistent with its own.
• After being informed of a change in the
concrete subject, a ConcreteObserver object
may query the subject for information
Participants
• The observer pattern has four participants.
• Subject – interface or abstract class defining the
operations for attaching and de-attaching observers to
the subject.
• ConcreteSubject – concrete Subject class. It maintain
the state of the object and when a change in the state
occurs it notifies the attached Observers.
• Observer – interface or abstract class defining the
operations to be used to notify this object.
• ConcreteObserver – concrete Observer
implementations.
Related Patterns
• Mediator: By encapsulating complex update
semantic s, the ChangeManager acts as
mediator between subjec ts and observers.
• Singleton: The ChangeManager may use the
Singleton pattern to make it unique and
globally accessible.

You might also like