The Memento Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.

Slides:



Advertisements
Similar presentations
Fields, Constructors, Methods
Advertisements

Slide 10.1 Advanced Programming 2004, based on LY Stefanus’s Slides Object Serialization Object serialization: the process of converting an object into.
Computer Science 313 – Advanced Programming Topics.
Memento Kendra Kachelein Maureen Thomas. Definition The memento captures and externalizes an object’s internal state, so the object can be restored to.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Classes, Encapsulation, Methods and Constructors
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Memento Page 283 Jason Penny. Memento Behavioral Pattern Behavioral Pattern Intent: Intent: Without violating encapsulation, capture and externalize an.
Behavioral Pattern: Memento C h a p t e r 5 – P a g e 179 Objects frequently expose only some of their internal state using public methods, but there.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
1 Binary Files ผศ. ดร. หมัดอามีน หมันหลิน Faculty of IST, MUT
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VII Observer, Command, and Memento.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
Lecture 19 Serialization Richard Gesick. Serialization Sometimes it is easier to read or write entire objects than to read and write individual fields.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Ch- 8. Class Diagrams Class diagrams are the most common diagram found in modeling object- oriented systems. Class diagrams are important not only for.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
©SoftMoore ConsultingSlide 1 Serialization. ©SoftMoore ConsultingSlide 2 Serialization Allows objects to be written to a stream Can be used for persistence.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Bridge Pattern (Structural) “All problems in computer science can be solved by another level of indirection.” – Butler Lampson ©SoftMoore ConsultingSlide.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Data Access Object Pattern (Structural – Not a GoF Pattern) ©SoftMoore ConsultingSlide 1.
The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1.
The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
The Flyweight Pattern (Structural) ©SoftMoore ConsultingSlide 1.
The Singleton Pattern (Creational)
The Proxy Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
The Object Pool Pattern (Creational – Not a GoF Pattern) ©SoftMoore ConsultingSlide 1.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
Overview of Behavioral Patterns ©SoftMoore ConsultingSlide 1.
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Jim Fawcett CSE776 – Design Patterns Summer 2005
Memento Pattern F 羅世東 F 李天彥 F 鄧義佐 F 林尚霖
Memento Design Pattern
Java Review: Reference Types
Encapsulation & Visibility Modifiers
The command invocation protocol
Serialization and Deserialization Bullet points from Head First Java, Ch Dec-18 serialization.ppt.
Prototype Pattern 1.
Review: Design Pattern Structure
The iterator and memento patterns
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Fundaments of Game Design
Mobile Programming Dr. Mohsin Ali Memon.
Memento Pattern 1.
Presentation transcript:

The Memento Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Memento: Basic Idea Sometimes it is necessary to record the internal state of an object; e.g., when implementing checkpoints and undo mechanisms. You must save the state information somewhere so that you can restore objects to their previous state. But objects normally encapsulate some or all of their state, making it inaccessible to other objects and impossible to save externally. A memento is an object that stores a snapshot of the internal state of another object, the memento’s originator. ©SoftMoore ConsultingSlide 2

Memento: Basic Idea (continued) The originator initializes the memento with information that characterizes its current state. Only the originator can store and retrieve information from the memento – the memento is “opaque” to other objects. ©SoftMoore ConsultingSlide 3

Memento Pattern Intent: Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later. Also Known As: Token Slide 4©SoftMoore Consulting

Memento Pattern (continued) 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 therefore would break the object’s encapsulation. ©SoftMoore ConsultingSlide 5

Memento Pattern (continued) ©SoftMoore ConsultingSlide 6 Caretaker Structure Originator state setMemento(Memento m) createMemento() state = m.getState() Memento state getState() setState() return new Memento(state) memento

Memento Pattern (continued) Participants Memento –stores internal state of the Originator object. –protects against access by objects other than the originator. narrow interface for Caretaker (can only pass the memento to other objects wide interface for Originator (lets the originator access all data necessary to restore itself to its previous state) Originator –creates a memento containing a snapshot of its current internal state. –uses the memento to restore its state. ©SoftMoore ConsultingSlide 7

Memento Pattern (continued) Participants (continued) Caretaker –is responsible for the memento’s safekeeping. –never operates on or examines the contents of a memento. ©SoftMoore ConsultingSlide 8

Memento Pattern (continued) Collaborations A caretaker requests a memento from an originator, holds it for a time, and passes it back to the originator ©SoftMoore ConsultingSlide 9 m : Memento: Caretaker createMemento() : Originator setMemento(m) «create» setState() getState()

Memento Pattern (continued) Consequences: The Memento pattern Preserves encapsulation boundaries. Memento avoids exposing information that only an originator should manage but that must be stored outside the originator. Simplifies Originator. Moves the storage management burden from the Originator to the clients. Having clients manage the state they ask for simplifies Originator. Can be expensive to use. Memento can incur considerable overhead if the originator must copy large amounts of information or if clients create/return mementos to the originator frequently. ©SoftMoore ConsultingSlide 10

Memento Pattern (continued) Consequences (continued) Defines narrow and wide interfaces. It may be difficult in some languages to ensure that only the originator can access the memento’s state. Can have hidden costs. A caretaker can incur large storage costs when it stores mementos. ©SoftMoore ConsultingSlide 11

Memento Pattern (continued) Implementation Language support. Mementos have two interfaces, a wide one for originators and a narrow one for other objects. C++ supports this requirement by letting you make Originator a friend of Memento and making Memento’s wide interface private. Only the narrow interface should be declared public. Storing incremental changes. The memento can save just the incremental change to the originator’s internal state rather than the full state of every originator object. ©SoftMoore ConsultingSlide 12

Memento Pattern in Java Objects that implement interface S erializable (from package java.io ) can be viewed as examples of the Memento pattern. –allows objects to be written to a stream –can be used for persistence (writing to a file stream) or for socket communication Serializable is an empty interface – just a marker. Classes that implement the interface are marked as serializable. ©SoftMoore ConsultingSlide 13

©SoftMoore ConsultingSlide 14 Java Serialization/Deserialization Serialization: write object as a sequence of bytes to a stream Deserialization: recreate brand new object on the other end with the original object’s data Note: Deserialization does not call the default constructor. It simply creates a blank object and fills in the fields with values retrieved via deserialization.

©SoftMoore ConsultingSlide 15 Serialization (continued) Object Serialization Java Application Class A instance int x = 4 B b = Class B instance Serialized Object(s) in Arbitrary File Java Application Class A instance int x = 4 B b = Class B instance DeserializationSerialization JVM External Storage

Memento Pattern in Android Android provides a “Memento-like” class called a Bundle that is used in conjunction with lifecycle methods to save/restore the state of an activity. Under low memory conditions, any activity not in the foreground can be killed. When an activity is about to be killed, the Android runtime calls the activity’s onSaveInstanceState() method, passing a Bundle parameter that can be used to record state information that needs to be saved. When an activity is recreated, the Android runtime calls the activity’s onRestoreInstanceState() method, passing it the previously saved Bundle object. ©SoftMoore ConsultingSlide 16

Related Patterns Commands can use mementos to maintain state for undoable operations. Mementos can be used to represent the current state of an iteration. ©SoftMoore ConsultingSlide 17

References Memento pattern (Wikipedia) Memento Pattern (Object-Oriented Design) Memento Design Pattern (SourceMaking) Memento (dofactory) ©SoftMoore ConsultingSlide 18