So guys, we are moving forward to over next design pattern. And before I start this pattern, let me tell you that we are inside the third section of our course. And these kind of patterns, which we are going to learn now are actually behavioral design patterns. So I hope you guys are pretty much clear with the creational patterns and structural patterns. Now it's time to move on with the behavioral design patterns. And the first pattern which we are going to see in this section is a command pattern.
Now, if I go through the slides of this particular pattern, this pattern is going to allow us to encapsulate an action within an object. Now we know what is encapsulation. And now we are going to encapsulate the actions within the objects actually. Now if you want to understand this command pattern, the first question which I want to ask you is that just you try to imagine that barrier using commands in the real life. Or I think the word command is very familiar with us, because I'm sure you all are familiar with command prompt. Now everybody, when somebody learn the computer first time, I'm sure that everybody has used command prompt.
And when they use Command Prompt, we fire commands. Like for example, I'm sure that you are familiar with the command Dir. Now, when we hit a dir command, and if I press enter, then after that is going to give me a list of directories in that particular drive. Now, when we fire this command, we know that this command is going to work. But the question which I want you to answer is, what if I type directories? Now, if you're saying that directories is not going to work, then you're right actually, because directories is not a command.
When somebody has developed this command prompt, they have decided some words and on this words they have defined some actions. So when we give this word is going to execute direction, the same kind of thing we are going to do in our application using this command design pattern. So, in this command design pattern, we are going to define some user defined words. And we have to define which kind of actions we want to do in this. In our case, I have taken a scenario we are going to have a two applications, one is going to be a center application and the other one is going to be a receiver application. Now, we are going to do something similar to the daily life like when you press the buttons of your TV remote is going to send some commands to the television and based on that television is going to do the operations like when you press the on off button or if you are increasing the volume or decreasing the volume, all the things are happening through the commands which are going to be sent from remote to the television.
In our case also we are going to create an application which is going to be a standard application which is going to work like a remote and the receiver application is going to be something like a television where we are going to receive the command and based on that will take some actions. Let's understand the scenario with the code sample. In my Visual Studio, I have created one solution. And inside the solution, I have already created two projects. If you check, I have a Fender app, which is my first application. And the center app is going to be Windows Forms application.
So you can see I already have one form one.cs inside this. And this is actually just now empty. So we do not have edit anything inside this application. So we have not added anything inside this application. The receiver app is a really simple console app. So inside the receiver application, we just have program.cs file, and this program.cs is also empty right now, we do not have any code inside this.
Now step by step we are going to create a structure inside the sender at first, and then we'll see how we can create a receiver app using that. In the center app, the first thing which I want to do is I'm going to right click on the center app. And I'm going to add a new C sharp class with the name television. This is the center app. So just keep one thing in your mind that this is going to be a remote, which is going to send the commands to the television. So we are going to create one class first with the name television.
And inside this television class, I'm going to make sure that this class is going to be public. And then the first thing is I'm going to add some nice bits on the top like this. Now I'm going to use some socket programming to communicate between these two applications. And that's why I have added three needs vs system.io, system dotnet and system dotnet dot sockets. We are going to use two classes of this namespaces in our code sample inside the C sharp class television. So that's why I have added the three namespaces on the top.
And inside this class. I'm going to have my code like this You can see that this television class is first having two private read only variables. One is IP address, which is of type IP address. And this is a predefined class of system dotnet, which can actually hold any IP address with the port numbers. And same way we have a port number which is going to be an integer variable. My constructor of the television class is actually defined in the below section.
And you can see we have two constructors. The first constructor is actually going to take parameters like IP address and port number. And whenever we are going to connect with television, it's showing me that we are going to provide two things that using which IP address you want to connect and which port number you want to use in that is going to do that injection inside this using the IP address and internally is going to pass the address and the port number. Whatever address and port number we are going to get from that parameter we are just going to store that thing in our private read only variables which are underscore IP address and underscore port So, we are just assigning these things into the private to the variables. The logic of this particular class is going to be there inside this one single method, which is sent.
And you can see that send method is going to have one parameter, which is going to be our command. The string command which is a parameter, we are going to define the number of commands later on. But right now, this method of the television is actually going to specify that we are going to send some commands using this. And inside this we are creating object of a predefined class TCP client. And this TCP client is allowing us to send some communicational messages using TCP protocol. We are certifying that using this client we are going to connect with the client on this particular IP and port number.
And once we do that thing, this client object is going to have the methods like get stream which is always going to connect with the client and he's trying to get the stream values from that using the We are just creating an object of stream writer, which is a class belongs to system.io. And using the stream writer whatever string we have passed into this client dot get stream, we are just going to write that thing. Once the writing part is done, we are going to do auto flows to true. And then we are just passing that particular command which we have called into our writeline. So, it's something like this that the Send method is just going to send some command. And on the other hand, using this TCP client object, we are going to print that particular command and then we are going to close the connection.
It's a very simple method, which is there inside this television class right now. And we are going to see that how we are going to create the command and how we are going to use the Send method into that. The next thing which I want to do in my project is I'm going to add a new C sharp class with the name power settings, and then I'm going to click on this class. Power setting is actually going to have one simple enum, which is going to allow me to make my application with the television on or off. So the code of this class is going to look like this. You can see that it's a simple enum, which is just going to have off and on to aliens inside that.
Once we are done with this, now it's the time to deal with our commands. And to do that thing, I'm going to create one interface. So right click on this application, and I'm going to add a new item. This is going to be my interface. And I'm giving the name of this interface which is going to be I command. This icon interface is going to be useful to hold my method.
So inside this we are going to have one simple method with the name execute. So I'm just making this interface public. And it's going to have the method execute inside this the logic of this execute method we are going to write inside our next class. Which is going to have my number of commands inside that. So I'm going to right click on this application one last time, and I'm going to add a new item, which is going to be a class. And I'm giving a name of this class my commands.
This Mike amongst class is going to implement our I command interface. And once we implement this interface into that, we know that we are going to get our method which is execute, we are making this class also public. And before we write a code inside this execute method, we need to declare some other properties in code inside this my commands. Now the logic of this my command is going to have number of lines of code inside this, which we'll do right now. The first thing which I'm doing inside this classes, I'm creating some constant variables on the top. So I'm declaring some condition inside this code that somewhere my command maximum length is going to be only four characters.
And same with the parameters which are going to pass is also going to be for critters and for this, I'm happy protected constants, which are going to have this values inside this, we have two string properties inside this which are actually having only get and having a private set inside that. So these two properties are having public and private set. And then we are actually having the third property which is going to be off type television. So this is where we have a connection between this command and our television class. Once we have this things declared on the top, it's a time to deal with our constructor of our mind commands class. And inside the constructor we axon, we're going to use our icon interface with that the constructor of my commands class is going to be looked like this.
So you can see that I have a mic amounts constructor, which is actually having three parameters inside that the string command string parameter which is going to be by default, no and it's going to be optional. And then we have a television which is also often parameter. So we are specifying the television by default is going to be null using these three parameters of the constructor. Now first we are checking that if the string which we are passing with this, which is there in the command is either null or empty or not. If this is empty we are going to throw an exception. Otherwise, whatever command we got inside that we are just going to trim and we're going to store inside the command then I'm checking that if the command dot length is not more than four characters using this command max length and if that is also not there, then we are going to move forward we are checking that if same way the parameters are empty or not.
And then the parameters max length is proper or not. So the same thing what we have done for the command we are checking the same kind of thing for the parameters. Once everything is perfect, we are going to pass on and assign this command into our command property, parameter property and the television property, the three properties which you created on the top. Once we are done with this, our customer looks fine. And now every time And I create an object of my my commands class, we are allowed to create our commands with the parameter for the specific television. So far, so good.
The last thing which we are going to do inside this class is we have to create commands. So now inside the mic amounts, we are going to create some of the commands. And in my case, I'm creating two commands. So you can see I have my commands and using this I have created two commands with the name volume and the power. But the volume and power both are command which are going to have three parameters called inside that you can see these are actually going to return the actual command name, which is vo lm. And then we are just passing the value into the string which is going to be my second parameter for this.
So that is going to be with a parameter, which I'm going to pass from my UI actually. And then the specific television with which I need to connect. Same way we have one more command which is power command and you In this we have given p o w r, because we can just pass up to four characters in that and then we are specifying the same way parameter as well as the television with that once we are done with this, we have created only two commands. While in your case you can create some more commands also later on. I'm going to check sending and receiving of the commands only by these two commands. And that's why Now it's the time to write the logic for the execute method.
My execute method looks like this. So you can see that inside this we are first checking that if television is equals to null, we are going to throw an exception. Otherwise, if television exists and we have a proper television, using the television we are going to call the method which we already defined inside the television class which is sent. And as we know that send method is going to take the command inside that as a parameter. So we are just passing to string inside that which is my overridden method of the to string, which is going to actually take command as well as parameter into my complex concatenate string. So this is going to give me a formatted string, which is going to have combination of command and the parameters of the command with that.
Once you're done with this, we are done with our sender application. And that's why let's build our solution. Once it's built succeeded, we are good to go to the receiver application. And let's see what application is actually going to receive my commands which I have created. The steps of the receiver application will do in our next video. Thank you