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.

Slides:



Advertisements
Similar presentations
The Stack Data Structure. Classic structure What is a Stack? An abstract data type in which accesses are made at only one end Last In First Out (LIFO)
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Win32 Programming Lesson 4: Classes and Structures.
Dynamic Allocation Eric Roberts CS 106B February 4, 2013.
1 Stacks Chapter 4. 2 Objectives You will be able to: Describe a stack as an ADT. Build a dynamic-array-based implementation of stacks. Build a linked-list.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
Memento Kendra Kachelein Maureen Thomas. Definition The memento captures and externalizes an object’s internal state, so the object can be restored to.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Stacks.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Memento Page 283 Jason Penny. Memento Behavioral Pattern Behavioral Pattern Intent: Intent: Without violating encapsulation, capture and externalize an.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
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.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
Controlling Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Inheritance Modes When deriving.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Pointers OVERVIEW.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Computing IV Singleton Pattern Xinwen Fu.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object Oriented Programming in C++ Chapter 6 Inheritance.
Behavioral Pattern: Strategy C h a p t e r 5 – P a g e 205 The Open-Closed Principle advocates designing software in such a way that it will absorb new.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Behavioral Pattern: Iterator C h a p t e r 5 – P a g e 159 Software can become difficult to manage when a variety of different traversals of a variety.
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Data Structures & Algorithms
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Memento Design Pattern Lee Lisle. Overview Why would we use Memento? Examples of Memento How to implement Memento.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
The Memento Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Templates. C++ 2 Outline Function templates  Function template definition  Function template overloading Class templates  Class template definition.
C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
Arrays as Function Parameters. CSCE 1062 Outline  Passing an array argument (section 9.3)  Reading part of an array (section 9.4)  Searching and sorting.
Lecture No.05 Data Structures Dr. Sohail Aslam.  Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Jim Fawcett CSE776 – Design Patterns Summer 2005
C++ Templates.
Creational Pattern: Prototype
Memento Pattern F 羅世東 F 李天彥 F 鄧義佐 F 林尚霖
Memento Design Pattern
The iterator and memento patterns
CMSC 341 Stacks and Queues 4/17/2019.
CMSC 341 Stacks and Queues 4/29/2019.
COP 3330 Object-oriented Programming in C++
CS 144 Advanced C++ Programming February 21 Class Meeting
Memento Pattern 1.
Presentation transcript:

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 are times when the entire state of an object needs to be saved because it might need to be restored later. In some cases, enough information could be obtained from the public interfaces to save and restore that data. In other cases, information that is not readily available needs to be saved. This sort of information saving and restoration is common in systems that need to support Undo commands. The Memento Pattern makes it possible to save and restore a previous state without making the object itself take care of this task, and without violating encapsulation.

The Memento Pattern C h a p t e r 5 – P a g e 180 The Originator creates a Memento containing a snapshot of its internal state, and uses the Memento to restore its internal state. The Memento stores as much or as little of the Originator’s internal state as necessary, at the Originator’s discretion, and protects against access by objects other than the Originator. The Caretaker is responsible for the Memento’s safekeeping, but never operates on or accesses the Memento’s contents. Mementos have effectively two interfaces. The Caretaker sees a narrow interface to the Memento -- it can only pass the Memento to the other objects. In contrast, the Originator sees a wide interface, one that lets it access all of the data necessary to restore itself to its previous state. Ideally, only the Originator that produces the Memento would be permitted to access the Memento's internal state.

C h a p t e r 5 – P a g e 181 Non-Software Example: Car Radio The driver (the Originator) sets the preset button and uses it to restore the previous state of the radio tuning. The preset radio button (the Memento) allows the radio to be restored to its desired state with a single button push. The radio (the Caretaker) provides access to the button, but is not capable of changing its state. The button eliminates the need for the drivers to memorize the radio frequencies of their favorite stations. The preset buttons store the information so that the tuning can be restored.

C h a p t e r 5 – P a g e 182 Stack Memento Code in C++ #include using namespace std; // The Memento takes a snapshot of the complete // contents of the stack, saving them in an array. class Memento { friend class Stack; public: ~Memento() { delete items; } private: int *items, size; Memento( int* list, int nbrValues ) { size = nbrValues; items = new int[size]; for (int i = 0; i < size; i++) items[i] = list[i]; } };

C h a p t e r 5 – P a g e 183 // The stack is the Originator. class Stack { public: Stack() { topIndex = -1; } void push( int in ) { items[++topIndex] = in; } int pop() { return items[topIndex--]; } bool isEmpty() { return topIndex == -1; } Memento* checkPoint() { return new Memento( items, topIndex+1 ); } void rollBack( Memento* mem ) { topIndex = mem->size-1; for (int i = 0; i size; i++) items[i] = mem->items[i]; } friend ostream& operator << ( ostream &output, const Stack &stk ) { string buf( "[ " ); for (int i = 0; i < stk.topIndex+1; i++) { buf += stk.items[i] + 48; buf += ' '; } buf += ']'; return output << buf; } private: int items[10], topIndex; };

C h a p t e r 5 – P a g e 184 // The main function is the Caretaker. void main() { Stack stk; int i; for (i = 0; i < 5; i++) stk.push(i); cout << "Original Stack: " << stk << endl; Memento* firstMemento = stk.checkPoint(); cout << "First Memento Created." << endl << endl; for (i = 5; i < 10; i++) stk.push(i); cout << "Revised Stack: " << stk << endl; Memento* secondMemento = stk.checkPoint(); cout << "Second Memento created." << endl << endl; cout << "Emptying Stack: "; while (!stk.isEmpty()) cout << stk.pop() << ' '; cout << endl; cout << "Emptied Stack: " << stk << endl << endl; stk.rollBack( secondMemento ); cout << "Stack After Second Memento Restored: " << stk << endl << endl; stk.rollBack( firstMemento ); cout << "Stack After First Memento Restored: " << stk << endl << endl; cout << "Emptying Stack: "; while (!stk.isEmpty()) cout << stk.pop() << ' '; cout << endl; delete firstMemento; delete secondMemento; }

Memento Pattern Advantages C h a p t e r 5 – P a g e 185 If all of the information describing an object is available in public variables, it is not that difficult to save them in some external store. However, making these data public makes the entire system vulnerable to change by external program code, when we usually expect data inside an object to be private and encapsulated from the outside world. The Memento Pattern attempts to solve this problem by having privileged access to the state of the object being saved. Other objects have only a more restricted access to the object, thus preserving their encapsulation. While supporting undo/redo operations is one significant use of the Memento Pattern, Mementos are also used in database transactions, in which they save the state of data in a transaction where it is necessary to restore the data if the transaction fails or is incomplete.