Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Memento Page 283 Jason Penny. Memento Behavioral Pattern Behavioral Pattern Intent: Intent: Without violating encapsulation, capture and externalize an."— Presentation transcript:

1 Memento Page 283 Jason Penny

2 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

3 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

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

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

6 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

7 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

8 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; //... };

9 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

10 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;}

11 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 };

12 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(); }


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

Similar presentations


Ads by Google