New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Example: the memento pattern

From: Foundations of Programming: Object-Oriented Design

Video: Example: the memento pattern

Next let's take a look at the Memento Design Pattern. This is a way of managing change, basically a way to undo a change to an object, but doing it in a way that does not violate encapsulation, and the idea that an object should always manage itself. I am going to illustrate this one without code. This Memento Design Pattern requires three classes that take certain roles, and we'll call them the Originator, the Caretaker, and the Memento. So we have the Originator. This is the object that you want to be able to change, and then undo any changes to.

Example: the memento pattern

Next let's take a look at the Memento Design Pattern. This is a way of managing change, basically a way to undo a change to an object, but doing it in a way that does not violate encapsulation, and the idea that an object should always manage itself. I am going to illustrate this one without code. This Memento Design Pattern requires three classes that take certain roles, and we'll call them the Originator, the Caretaker, and the Memento. So we have the Originator. This is the object that you want to be able to change, and then undo any changes to.

For you this could be a customer object, a spaceship object, whatever needs to be able to reverse out a change. But the question is who is changing it, who is affecting it? Well, then we have the object we refer to as the Caretaker. This object is going to deal with taking care of when and why the Originator needs to either save its state or to revert back to a previous state. Now, the Caretaker is often also the object that will actually be changing the Originator, so it's in a good position to know when it needs to save state.

So the Originator is in a particular state at the moment, and the Caretaker requests that the Originator save itself, and here is where the Memento object comes into play. The Originator creates a Memento object, which is a simple object that just details the important parts of the Originator, the information needed to return to a particular state. And it bundles that information up in the Memento and returns it to the Caretaker. And here's the thing. The Caretaker does nothing except store that Memento.

It doesn't look at the internal state of that object, it doesn't do anything with it, it just takes care of it. And then we issue a few calls to the Originator to change it. These calls could come from the Caretaker, they could come from elsewhere. And at any point the Caretaker could ask again for another Memento, if we wanted to keep multiple levels of undo. But the Originator is not encumbered by trying to keep track of multiple levels of state inside itself. And if we want to revert to a particular state, the Caretaker just hands back the Memento object to the Originator and asks it to restore itself.

So we're not breaking encapsulation, the Originator is still the object that takes care of its own internal state. We're not reaching in from the outside and starting to tweak all the different attributes. But neither does the Originator need to keep its own stack of changes to itself. As you can see, with this one, this could be implemented in virtually any Object-Oriented Design language, and this is the Memento Design Pattern. When you start to explore Design Patterns, particularly simple ones like Memento, it's common to occasionally think, well, I could have come up with that idea myself.

And really this is kind of the point, Design Patterns are situations that did arise naturally multiple times and they've been proven to work just worth putting down on paper so that they become common knowledge. And while it's true that most Design Patterns are more complicated than Memento, hopefully you now have a good idea of where they fit in the Object-Oriented Design process.

Show transcript

This video is part of

Image for Foundations of Programming: Object-Oriented Design
Foundations of Programming: Object-Oriented Design

47 video lessons · 46406 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 11m 35s
    1. Welcome
      1m 25s
    2. Who this course is for
      1m 15s
    3. What to expect from this course
      3m 6s
    4. Exploring object-oriented analysis, design, and development
      1m 41s
    5. Reviewing software development methodologies
      4m 8s
  2. 26m 14s
    1. Why we use object-orientation
      2m 42s
    2. What is an object?
      5m 22s
    3. What is a class?
      4m 43s
    4. What is abstraction?
      2m 45s
    5. What is encapsulation?
      3m 45s
    6. What is inheritance?
      3m 35s
    7. What is polymorphism?
      3m 22s
  3. 12m 16s
    1. Understanding the object-oriented analysis and design processes
      4m 13s
    2. Defining requirements
      6m 9s
    3. Introduction to the Unified Modeling Language (UML)
      1m 54s
  4. 23m 35s
    1. Understanding use cases
      6m 11s
    2. Identifying the actors
      4m 16s
    3. Identifying the scenarios
      5m 7s
    4. Diagramming use cases
      4m 18s
    5. Employing user stories
      3m 43s
  5. 16m 36s
    1. Creating a conceptual model
      1m 59s
    2. Identifying the classes
      2m 27s
    3. Identifying class relationships
      2m 38s
    4. Identifying class responsibilities
      6m 43s
    5. Using CRC cards
      2m 49s
  6. 22m 25s
    1. Creating class diagrams
      6m 11s
    2. Converting class diagrams to code
      4m 57s
    3. Exploring object lifetime
      5m 55s
    4. Using static or shared members
      5m 22s
  7. 19m 49s
    1. Identifying inheritance situations
      6m 49s
    2. Using inheritance
      2m 43s
    3. Using abstract classes
      2m 2s
    4. Using interfaces
      4m 20s
    5. Using aggregation and composition
      3m 55s
  8. 9m 23s
    1. Creating sequence diagrams
      5m 18s
    2. Working with advanced UML diagrams
      2m 3s
    3. Using UML tools
      2m 2s
  9. 10m 39s
    1. Introduction to design patterns
      2m 40s
    2. Example: the singleton pattern
      4m 53s
    3. Example: the memento pattern
      3m 6s
  10. 21m 47s
    1. Introduction to object-oriented design principles
      2m 50s
    2. Exploring general development principles
      3m 55s
    3. Introduction to SOLID principles
      6m 43s
    4. Introduction to GRASP principles
      8m 19s
  11. 7m 1s
    1. Reviewing feature support across different object-oriented languages
      3m 50s
    2. Additional resources
      2m 27s
    3. Goodbye
      44s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.