Lecture thumbnail 0:00 / 0:00 Okay.

So we’re going to take a look at how to build the command design pattern.

And we’re going to use an example that I think we used before, the example of a bank account.

Now, in the previous uses of bank account, we had withdrawal and deposit methods that you called on

the account.

Now things are going to be different because we might want to implement some sort of audit mechanism

or we might simply want to record all the transactions that are happening on a bank account.

So we’re going to have the bank account as before.

However, we’re going to manipulate the bank account using command classes.

So let’s make a class called Bank Account.

And here I’ll have the balance.

I will also have some sort of overdraft limit.

So int overdraft limit, let’s set it to -500 for example.

And then I will have the deposit and withdrawal methods as before.

So deposit simply adds a certain amount to a bank account.

So I’ll specify the amount as the argument here will increase the balance by the amount like so.

And in addition, I’m going to do a right line just to inform everyone what’s going on.

So let me do a using static system console here so that we can use right lines.

So we’re going to write line.

What are we going to write line?

Well, we’re going to say deposited a certain number of dollars, which is the amount and the balance

is now and we specify the balance here.

So there we go.

That’s our line of information.

And then we’re going to have another method called withdraw.

And this method is going to check whether we have enough money to actually withdraw the specified amount.

And if we do, then we can withdraw it.

So we say if balance minus amount is greater than or equal to the overdraft limit, then you can withdraw.

And we say balance minus equals amount.

And we will once again write line something interesting.

So we’re going to say withdrew dollar amount.

And yes, we need the dollar in front of the string as well.

So withdrew dollar amount and the balance balance is now and balance.

There we go.

Okay.

So now that we’ve got these two methods implemented, we can build a structure for a command which will

be sent to some agent, maybe the account itself, in order to modify the account.

So we’re going to have a class called well, actually, no, let’s make an interface.

We don’t need any concrete details here.

Let’s make an interface called I Command.

There we go.

And we’re going to have just a single method to begin with.

We’ll have a void call so you can call the command and this effectively executes it.

So let’s make a concrete command.

And here we’re going to make a bank account command, which is going to implement this I command interface.

I command.

There we go.

So we’ll import the members.

And what are we going to do here?

Well, first of all, we need to decide what the bank account command needs to know.

And first of all, it needs a reference to the bank account.

So let’s have that account.

In addition, we need to know what kind of action to perform, whether this command is for depositing

or withdrawing.

So once again, I’ll just make an enum here.

Let’s make an enum like this and I’m going to call this enum action.

So we’re going to have two actions.

It’s either deposit or withdraw.

And then of course, we’ll have a member of this enum.

So I will have action, action like so.

And in addition I will have the amount that we want to either withdraw or deposit.

So now that we have all of these things, we can build a constructor which actually initializes all

of these.

Once again, I’m going to get rid of this pesky annoyance joining the two together and then of course

we can implement the call.

So what happens is we check which kind of action is required.

So we switch on the action like so, and then let me generate the labels here.

So if we are depositing, all we do is we say account dot deposit.

Amount and similarly for the withdrawing will say account withdraw and once again specify the amount.

There we go.

So we seem to have a bit of a problem here.

Oh, yes, of course, the brakes are missing, so let’s add the brakes in as well.

Okay, So this is good.

And next up, what we can do is we can actually start using all of this infrastructure.

But before we do, let’s.

Well, actually, yeah, I think we’re ready.

I think we’re good.

So I’ll make a bank account.

Var ba equals new bank account.

There we go.

And in addition, I’ll make a bunch of commands.

Now I don’t need just a single command.

Maybe I want to process a batch of several commands.

So I’ll say var commands equals new list of bank account command.

There we go.

And here we’ll have two commands, one for depositing and one for withdrawing.

So I’m going to have a deposit of 100.

So I’m going to have a new command.

And here we have the accounts we need to specify the account.

The action is going to be deposit and the amount is going to be, well, let’s have 100 like so and

then I’m going to withdraw 50 as well.

So another new bank account command like so bank account.

Once again, the action this time around is withdrawal and the amount is 50.

So we expect $50 at the end of all of these transactions.

First of all, let’s output well, let’s output the balance of the current bank account.

Actually, I’m going to make a two string for it.

So bank account, let’s implement formatting members like so.

And here all I want to use is the balance.

Oops, let’s try this again.

So formatting members, get the balance in here and that’s pretty much it.

That’s all I want from the account.

So now I can write line the account write line and we can already run this and well, we can run this,

but it’s not doing anything at the moment.

It’s not processing the command.

So it should just say balance zero because that’s what we have in the account to begin with.

Okay.

And that’s exactly what we get.

So now we can go ahead and we can take all the commands and apply them in sequence so we can say for

each var C in commands and we can say, please apply this command.

So we can say command dot and then call, simply call the command, and then we can write line B Once

again, we should get some diagnostic information in the output.

So here we go.

We have a balance of zero.

We deposited 100.

So the balance is 100.

We withdrew 50.

So the balance is now 50.

So this is a very simple example of how you can implement the command pattern.

And instead of sending information request imperatively by calling methods, you actually put in commands.

And the reason why this is great is because you now have a list of commands that were executed.

So if you wanted to roll back those commands or if you wanted to search for a particular command, then

this information isn’t lost.

This information is available in some sort of data store.

And here instead of a list, you could have a database.

So you could have a real storage somewhere on disk where you can store all the transactions which happened

and then you can sort of inspect them and maybe correct for incorrect transactions and so on and so

forth.

The only thing I’ll mention is that in our implementation we made the deposit and withdraw methods public.

Of course, if you are doing the command pattern, you might want to avoid it.

You might want to make them internal, for example, so that whoever is operating on the bank account

from the outside doesn’t have the ability to change it directly, because obviously that will throw

a spanner in the works of all the command processing that we’ve done here.

Play Stop Play Play Start Play