Memento Page 283 Jason Penny. Memento Behavioral Pattern Behavioral Pattern Intent: Intent: Without violating encapsulation, capture and externalize an.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Chapter 14 Graph class design John Keyser’s Modifications of Slides by Bjarne Stroustrup
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Software Engineering Class design. Class design – ADT Abstract Data Types:  Why use ADT? Hidden implementation details Changes do not affect whole program.
Computer Science 313 – Advanced Programming Topics.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Memento Kendra Kachelein Maureen Thomas. Definition The memento captures and externalizes an object’s internal state, so the object can be restored to.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Software Design & Documentation – Design Pattern: Command Design Pattern: Command Christopher Lacey September 15, 2003.
Essentials of interaction diagrams Lecture Outline Collaborations Interaction on collaboration diagrams Sequence diagrams Messages from an object.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
Inheritance and Polymorphism CS351 – Programming Paradigms.
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.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Architectural Styles, Design Patterns, and Objects Robert T. Monroe (Doctoral Candiate) Andrew Kompanek (Research Programmer) Ralph Melton (Graduate Student)
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
POSL (Principles of Software Languages) Gr. Kyushu Institute of Technology, Japan Pointcut-based Architectural Interface.
Pattern Hatching - John Vlissides Pages 85 – 101 Todd Anderson
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VII Observer, Command, and Memento.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Objects to Protect Private Details: Memento and Iterator Snarf the iterator code for today’s class.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Joe Boylan David Guzman Oscar Ontiveros Fabian Pizana Jose Segura Adrian Veliz.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
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 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Memento Design Pattern Lee Lisle. Overview Why would we use Memento? Examples of Memento How to implement Memento.
© 2004 Pearson Addison-Wesley. All rights reserved October 31, 2007 Static Class Members ComS 207: Programming I (in Java) Iowa State University, FALL.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Class Diagrams. Terms and Concepts A class diagram is a diagram that shows a set of classes, interfaces, and collaborations and their relationships.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
The Memento Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
CSE 432: Thanks for the memory leaks, Pushme-Pullyu, and Command Summary of “Thanks for the Memory Leaks” “Left-over” design forces from Type Laundering.
Abstract Factory Pattern Jiaxin Wang CSPP Winter 2010.
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
Design Patterns C++ Java C#.
Design Patterns C++ Java C#.
Design Patterns Part 2: Builder & Memento
Memento Pattern F 羅世東 F 李天彥 F 鄧義佐 F 林尚霖
Memento Design Pattern
Doug Jeffries CS490 Design Patterns May 1, 2003
Classes and Objects Encapsulation
PH Chapter 3 Thanks for the Memory Leaks Pushme-Pullyu (pp
Review: Design Pattern Structure
The iterator and memento patterns
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Memento Pattern 1.
Software Design Lecture : 38.
CSG2H3 Object Oriented Programming
Presentation transcript:

Memento Page 283 Jason Penny

Memento Behavioral Pattern Behavioral Pattern Intent: Intent: Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later Sometimes its necessary to record the internal state of an object Sometimes its necessary to record the internal state of an object When implementing checkpoints When implementing checkpoints When implementing undo mechanisms for both backing out of tentative operations and for recovering from errors When implementing undo mechanisms for both backing out of tentative operations and for recovering from errors

Problem: We don’t want objects to expose their details to provide save and restore functionality, this would violate encapsulation We don’t want objects to expose their details to provide save and restore functionality, this would violate encapsulation A memento is a copy of the minimal information required to rebuild the object’s state, so large objects can be rebuilt using smaller mementos A memento is a copy of the minimal information required to rebuild the object’s state, so large objects can be rebuilt using smaller mementos

Memento Class Diagram state = memento->getState() Caretaker Originator createMemento() setMemento() state Memento getState() setState() state return new Memento(state)

Memento Interaction Diagram aCaretakeranOriginatoraMemento createMemento() new Memento setState() getState() setMemento(aMemento)

Supporting Undo Sometimes reversing the actions on an object does not return the object to its previous state Sometimes reversing the actions on an object does not return the object to its previous state Consider a connected line class that insures that a line always connects the two rectangles it is associated with Consider a connected line class that insures that a line always connects the two rectangles it is associated with

Reversing the Operations In this case, reversing the operation produces a different result than we started with Using a memento to save and restore the state solves this problem

Implementation class State; class Originator { public: Memento* CreateMemento(); void SetMemento( const Memento* ); //... private: State* _state; // internal data structures //... }; class Memento { public: // narrow public interface virtual ~Memento(); private: // private members accessible // only to Originator friend class Originator; Memento(); void SetState( State* ); State* GetState(); //... private: State* _state; //... };

Summary: Consequences Preserving encapsulation boundaries Preserving encapsulation boundaries It simplifies Originator It simplifies Originator Using mementos might be expensive Using mementos might be expensive Defining narrow and wide interfaces Defining narrow and wide interfaces Hidden costs in caring for mementos Hidden costs in caring for mementos

Sample Code class Graphic; // base class for graphical objects in the graphical editor // base class for graphical objects in the graphical editor class MoveCommand { public: MoveCommand( Graphic* target, const Point& delta ); MoveCommand( Graphic* target, const Point& delta ); void Execute(); void Execute(); void Unexecute(); void Unexecute();private: ConstraintSolverMemento* _state; ConstraintSolverMemento* _state; Point _delta; Point _delta; Graphic* target; Graphic* target;}

Sample Code Continued class ConstraintSolver { public: static ConstraintSolver* Instance(); void Solve(); void AddConstraint( Graphic* startConnection, Graphic* endConnection ); void RemoveConnection( Graphic* startConnection, Graphic* endConnection ); ConstraintSolverMemento* CreateMemento(); void SetMemento( ConstaintSolverMemento* ); private: // nontrivial state and operations for enforcing // connectivity semantics };

void MoveCommand::Execute() { ConstraintSolver* solver = ConstraintSolver::Instance(); _state = solver->CreateMemento(); // create a Memento _target->Move( _delta ); solver->Solve(); } void MoveCommand::Unexecute() { ConstraintSolver* solver = ConstraintSolver::Instance(); _target->Move( -_delta ); solver->SetMemento( _state ); // restore solver state solver->Solve(); }