Lecture thumbnail 0:01 / 3:06 It is section of the course.

We’re going to take a look at the command design pattern.

So what is the motivation for using a command?

Well, think about a typical C sharp statement.

Let’s suppose that you say X dot foo, for example, or you say X dot y is equal to five.

The problem with these statements is that they’re perishable.

And what I mean by perishable is that once you, let’s say you write to a field or you write to a property,

you cannot just go ahead and undo it.

There is no functionality built right into the c sharp programming language to let you unassign a reference

that you’ve just assigned or unwrite a particular field or property.

It’s simply not possible.

Now you cannot, in a similar way, directly serialize a sequence of actions or calls.

Now, strictly speaking, using expressions and the expression of type, you can serialize lambdas to

a degree, but really this is not as easy as it sounds.

Even though some functionality has been added to make sure that expression trees can be serialized,

it’s still quite a bit of a headache.

So what we really want is we want some sort of simple way of having objects which actually represent

operations.

So for example, you would have a separate object which states that X should change its property Y to

the value of Z, or that X should do W, where W is a method.

Now, the uses of this design pattern are numerous because, for example, gooey commands.

So whenever you press control s or you go into the menu and choose file and then save, you’re doing

the same thing in both cases.

And that thing is sending a command.

Once again, the command pattern is something that lets you accomplish a multi-level undo redo functionality

or if you want your application, for example, to allow the users to record and replay macros.

And obviously those macros have to be actions that can be encapsulated.

And the command pattern is exactly the construct which is used to encapsulate all of those ideas.

So the command design pattern essentially lets you build an object which represents an instruction to

perform a particular action.

And this command contains all the information necessary for the action to be taken.

Now, there is a bit of a philosophical point here as to whether queries should be included as part

of the command pattern or not, because on the one hand, it’s kind of a bit of a mismatch because we

use this term command query separation.

So commands are separate from queries.

On the other hand, the approach which we’re doing here, the approach of actually collecting the set

of invocations on a particular type, in this case it’s commands, but it could equally be queries is

available to either or.

So It’s really, I would argue that the queries in the traditional sense where you send a request for

a piece of information if you need to persist those queries, if you need to, for example, review

those queries and perform audits on them, then those queries also fall under the command design pattern.

Stop Play Play Play Start Play information alert