Lecture thumbnail 0:01 / 1:41 In this section of the course.
We’re going to take a look at the memento design pattern.
And the goal of the memento is to keep some sort of token, which then allows you to return an object
to a particular state.
So what is the motivation for using Memento in the first place?
So as you probably know, if you have a system with mutable components, then an object or indeed a
set of objects goes through a set of changes.
So for example, you have a bank account and the bank account gets participated in things like deposits
and withdrawals and that sort of thing.
So as a consequence, there are different ways of actually navigating the set of changes.
And we already looked at some of them because one of the ways is to record every single change in the
bank account, and that would be the command pattern.
And then you can teach the commands to undo themselves and thereby rolling back the entire sequence.
But another approach is to simply save the snapshots of the system at particular points in time, and
then allow the user to roll back the system to a particular snapshot.
So in this case, what a memento is, it’s a kind of a token or a handle that you return from a method
typically, and this represents the system state and it allows us to roll back to the state when the
token was actually generated.
Now the token may or may not directly expose state information, but it’s typically an immutable construct.
So it may provide a certain amount of information about the state of the system, but it doesn’t allow
you to actually change the system unless you take this token and you stick it back into the system explicitly
asking it to roll itself back to that particular token.
Stop Play Play Play Start Play information alert