So we're now going to get acquainted with the command design pattern.
And for this we're going to use a scenario that's been done to death by every person everywhere and
that is the bank account so we're going to simulate a bank account and in the bank account we'll have
a starting balance and we'll also have an overdraft limit.
You're off limits let's say minus 500.
So you can borrow up to $500 from the bank.
OK.
So we're going to have deposit and withdrawal methods simulating taking money out of the account and
putting money into the account.
So for the deposit we take the amount and we simply increase the balance by the amount.
Well let me get my usual headers in here so I have everything ready.
There we go.
And then I'm also going to see with a couple of messages so I'm going to say that I deposited a certain
amount and the balance is now and then it's in the balance.
No I go OK.
I should probably break it half way.
So out of here.
OK.
So that's the deposit function.
Now we can also do with the withdrawing function that's when you take money out of the account so here
we're going to call it withdrawal specifying once again the amount to withdraw and here we need to check
whether or not we have enough money including the overdraft.
So if balance minus amounts is greater than or equal to the overdraft limit then we decrease the balance
by the amount and we can also put to death and other diagnostic message.
So just like this one let me actually copy this over but this time it's a withdrawal.
So we withdrew a certain amount and the balance is now whatever the balance is we go.
OK so we want to manipulate this bank account and we might want to also record the fact that the bank
account is manipulated.
So we're going to create extra objects which are going to be called commands for actually performing
this operation so we're going to have a command which modifies the bank account in a particular way
depositing or withdrawing money and that you can also save somewhere there by having an audit log of
everything that's happening in the system.
So let's define an interface I'll call it command.
And for now I'm just going to have a single function called coal.
So the idea is that whoever implements this command has a function called Call.
And when you call this function that's where the command is actually invoked and gets to modify the
object that it's attached to.
So in this case we'll have a structure called bank account commands of which is going to implement the
command interface.
And so what do we need in the bank account command.
Well we need a reference to the bank account that we're going to operate on.
So that's going to be bank accounts or reference accounts.
We'll also define the kind of action that we want to perform whether we want to deposit money or withdraw
money.
And this is going to be called action.
And finally we'll specify the amount that we want to withdraw our deposit.
So now we can build a constructor which initializes all of these fields fairly obvious stuff and then
we need to implement the call function that goes up inside the call what we need to do is we need to
check what kind of action has been requested.
So we're going to switch on the action and hopefully generate the cases automatically using the ID.
So we have two cases deposit and withdraw.
And this is well before the operation.
So if you want to deposit you say accounts that deposit a certain amount and if you want to withdraw
then you say accounts dolt withdraw that amount.
That's all there is to it.
That's how the command can look like.
And we can now start using this command so if we go into the main function here we can make a bank account
first of all.
And then oh we can invoke a single command on it or indeed we can go for a number of commands.
We can actually make for example several commands that we want to invoke.
So make a vector of bank accounts command objects it's called commands and here I'm going to initialize
a bank account command with a reference to the bank account where we're going to deposit $100.
So I will say bank account command deposit and we deposit a hundred.
And then let's have another command where once again we pass a reference to the bank account.
And this time round we're going to withdraw a certain amount to try to withdraw let's say 200 for example
that's not going to work obviously because we don't have two hundred but it's about that operation to
do.
So now what we can do is first of all we can print out the bank account as it is.
Let me actually just generate a stream up with the operator for it.
So we're going to just output the.
Actually no it's not for the command it's for the bank account itself.
Here we go let's have stream output operator here.
The only thing we need to show is the balance.
So that's pretty much it.
And now coming back here what I can do is I can print's the bank account and then I can go through each
of the commands I can process them in turn.
And then once again show the balance Afshar that's been updated.
So for auto CMD the commands I can take each of the commands and I can call it.
So I call the command.
Each command has a reference to the bank account so it know what to operate on.
And then we can print out the bank account after this is done.
So a very small implementation here.
And let's take a look at what this actually gives us.
That.
So as you can see we're starting out with a balance of zero with deposit a hundred so the balance is
now a hundred.
And then we withdraw two hundred.
And yes I forgot about the overdraft so the overdraft lets us go to minus 100 in balance and that's
our final balance.
So this is how you implement a very simple command structure.
And in the subsequent lessons when we're going to do is we're going to try and enhance this approach
and look at what we can do with this newfound approach to handling changes in the bank account.