Node js Design Pattern, The Command pattern

With the command pattern, commands are objects that contain all of the datanecessary to execute a specific action on the client. Think about boxes of furniture that need to be assembled. Each box contains all of the parts necessary along with the instructions on how to assemble that piece of furniture.

A professional installer can open a box, follow theinstructions and use the materials to assemble the furniture. The installer doesn’t need to know how to build each piece of furniture. It doesn’t matter to them because the instructions of how to build the furniture come with the box.

The installer can open a box, follow the instructions and assemble the furniture; and then they can move on to the next box. In this analogy I like to think of each furniture package as a command. They are all different, they contain different parts and different instructions.

The installer can execute a command. They can follow the instructions, use the materials and complete the task. The Gang of Four defines the intent of the command pattern as encapsulating a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Let’s take a look at some code where we can incorporate a command pattern. So I’m inside of our Exercise Files folder, under Chapter 4, Chapter four, Lesson three, and within the Start folder you can see that we already have quite a bit of code. So let’s go ahead and run this code just to see what’s happening and come over to the terminal, node index.js from this location. And you can see we have a little application here that’s prompting the user to enter a couple commands. We can enter an Exit command and its says To Do, actually exit the application. We can also enter this Create command. So we can create a file like hello.txt Hello World.

So what we want this command to allow us to do is create a new text file called hello.txt and add Hello World to the contents of that text file. And again, you’ll see it says To Do, create text file hello.txt and the file contents, Hello World. So if I go ahead and exit out of this with Ctrl-C I’ll go ahead and clear the terminal for later. We can come back over to the code and let’s briefly talk about what’s happening. So we’re building the prompt using the create interface method from read line. And our process standard input and process standard output is going to be our interface.

And on line eight we are adding our first prompt. Whenever the user enters any text we go ahead and handle it with this line event. So the input comes in as their text. And lines 12-14 are just a matter of parsing that text to find the command and the actual file name or a file text that would go into that command. And then, finally, we have a switch statement where we are ready to run our commands. So if we run an Exit, we’ve left ourselves a little to do for adding an Exit command; and if we run a Create, we’ve left ourselves a little to do for running a Create command.

So we’ve already parsed the file name and the text that would go into the file that needs to be created for you. And then finally, if you don’t run either of these commands, we’re just letting you know that we don’t know what you’re trying to do. The command text that you’ve sent is not found. So what we’re going to do is we’re actually going to run these commands using the command pattern. So let’s take a look at what that might look like by adding the code to this file. So I’m going to go ahead and go to the top. The first thing that I need to bring in is something to run the command. I’m going to call that the Conductor. And we are going to go ahead and pull in the Conductor from the conductor.js file.

And then I’m going to go ahead and pull in a few commands. I’m just going to put all of the commands in the same file so I can de-structure them. I’m going to pull out the Exit command and I’m going to go ahead and pull out the Create command. So these are the two things that we need to be able to do, exit the application and create a new file. So we will package everything that we need to execute these commands into the command classes themselves.So everything we need to run an Exit will be found in the Exit command, and everything we need to create a new file is found in the Create command. And we will put both of these in a file called commands.js.

So using these commands will look a little something like this. We can come down here for the Exit command and we can just tell the Conductor to run a new Exit command. And this should cause us to actually exit the application. We can also come down here to the Create command and we can tell the Conductor to run a new Create command. And here’s the neat thing, the Create command is going to create a file so the file name should be sent to it’s constructor. That’s the file name we want to create and the text should also be sent to the constructor because that represents the contents that we want to create within that file.

So, in this example of what we just typed, we can see how we can use the command pattern.One of the best things about this pattern is that it is completely extensible. If we have a need for new commands, we can simply create a new command objects and run them with the conductor. In the next lesson, we will write the code for each command class, along with the conductor, so that this application will work.


Please enter your comment!
Please enter your name here