All right so let's take a look at the implementation of the memento design pattern.
So essentially the Memento is just a little token that gets returned from some API function call and
that allows you to return a system to that particular state.
So let me show you how this works so let's set up a typical bank account.
Now we've done this for a number of times already.
So here is a bank account we're going to have a balance which is going to be a private field.
Initially zero shall we say and then everything else will leave public so we'll have the constructor
which actually sets the balance.
And we're also going to have some function like deposit for example so you want to deposit a certain
amount of money.
So you want to deposit a certain amount and typically what you do is you just take the balance and you
incremented by the amounts fairly obvious stuff.
Now what you want to do is you want to save this information want to save this as a snapshot of the
bank accounts state.
So what do we do.
We can introduce a new class called Memento.
So this is going to be a snapshot of the bank accounts state capital at a particular point in time.
And in order to do this we have to save all the state of the bank account of the bank account really
only has one item a state that's the balance.
So we'll simply replicated here.
We'll make a constructor which takes the balance and well that's pretty much it.
Because subsequently all we need to do is we need to just return it at some point so instead of having
to pause it as a void function will have a function returning a memento and after we've changed the
balance we're going to return a memento which takes that balance as a constructive argument like so.
OK.
So we don't actually need to write the name mento here because the compiler can infer it from the return
type of the method itself.
So we are good to go here and now the next question is how do we actually restore the bank account to
the state predicated by the momenta.
So one way of doing it is making a function called Restore which takes a memento
and simply reads its balance and restores it to the balance so we say balance equals Amdocs balance
like so that's pretty much it except not quite.
You'll see I made the balance here private deliberately so that people couldn't manipulate it because
imagine if somebody got a memento changed it and then fed it back into the bank account that would be
problematic if you change it to a million for example.
So in order to get this private field to become accessible you have to declare a French class.
Now we've heard just this now we've already discussed some of the alternatives related to how you can
give access to private fields for example you could make a memento an entire class of bank account you
would have it as an asset class and that way this problem goes away.
But it's really up to you how to implement this.
I believe we discussed that when we talked about the builder and the factory pattern.
So similar idea here.
I'm just keeping it outside.
But obviously you have to have that friend class in order to be able to actually get the private field
in here.
But apart from that this is how we actually restore the bank account let's actually generate some sort
of stream output operator for our printing of the bank account and once again I got to remember to go
back here and delete that Allstream statement because that is just extremely annoying.
So we have stream out with operator here and what we can do now is we can actually operate on this bank
account.
We can get a snapshot using this memento pattern and we can use it to restore the bank account to a
previous state so here is a bank account with a certain balance of 100.
And I'll have to momentos I'll have em one that's going to be after the pausa off 50 all those sold
the account here will have 150.
And then let's have another one.
Badeau deposit twenty five.
So here the balance is 175.
So what we can do is we can first of all print out the bank account balance as it is and then we can
try and undoing to a particular momentos so we can actually use the memento as a kind of rollback mechanism
so I can say don't restore and I can restore to memento one.
And then we can print out the bank account once again actually out put some some message here back to
one that will go and we can also restore to them too.
And I'm going to say back to him to too.
All right.
So this is going to illustrate to us how you can jump back from one state to another.
So in a way what we're doing is we're doing a kind of undo reading mechanism but it's very discreet.
It's not a line of changes like when we looked at the command pattern it's not the line of changes where
you can go backwards and forwards you can go to discrete points of time that you've saved by saving
a memento of that point in time.
So let's actually execute this and see how this looks.
All right so as you can see we're starting out with a balance of 175 the memento one that we restore
to here it is.
So that should be 150.
And exactly that's what we have here.
And then memento 2 is the final value 175 and that's exactly what we restore to as well.
So we're essentially using memento as a way of saving the snapshots of a system at particular points
in time.
But notice that they're not immediately accessible so the memento doesn't allow you to modify the internal
states because then if you could do that you could restore the bank account to an invalid state so for
example you could get a memento.
So the balance to one billion or minus five for example or something like that or a nan and restore
the bank accounts of that.
So we don't want that so we don't actually allow any kind of manipulation on the memento itself so typically
a memento is an immutable object that is not an object that you can modify quite simply because it doesn't
make any sense.