Command Pattern Part 1

14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.14
List Price:  €93.07
You save:  €27.92
£55.73
List Price:  £79.62
You save:  £23.88
CA$95.61
List Price:  CA$136.60
You save:  CA$40.98
A$106.30
List Price:  A$151.87
You save:  A$45.56
S$94.64
List Price:  S$135.20
You save:  S$40.56
HK$546.91
List Price:  HK$781.33
You save:  HK$234.42
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr764.69
List Price:  NOK kr1,092.46
You save:  NOK kr327.77
DKK kr485.92
List Price:  DKK kr694.20
You save:  DKK kr208.28
NZ$117
List Price:  NZ$167.15
You save:  NZ$50.15
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,661.98
List Price:  ৳10,946.16
You save:  ৳3,284.17
₹5,839.65
List Price:  ₹8,342.71
You save:  ₹2,503.06
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,575.56
List Price:  ฿3,679.53
You save:  ฿1,103.97
₺2,262.43
List Price:  ₺3,232.18
You save:  ₺969.75
B$357.76
List Price:  B$511.10
You save:  B$153.34
R1,296.01
List Price:  R1,851.52
You save:  R555.51
Лв127.38
List Price:  Лв181.98
You save:  Лв54.60
₩95,113.23
List Price:  ₩135,881.87
You save:  ₩40,768.63
₪260.11
List Price:  ₪371.60
You save:  ₪111.49
₱3,999.61
List Price:  ₱5,713.97
You save:  ₱1,714.36
¥10,715.43
List Price:  ¥15,308.41
You save:  ¥4,592.98
MX$1,185.45
List Price:  MX$1,693.57
You save:  MX$508.12
QR254.79
List Price:  QR364.01
You save:  QR109.21
P955.69
List Price:  P1,365.33
You save:  P409.64
KSh9,427.65
List Price:  KSh13,468.65
You save:  KSh4,041
E£3,355.67
List Price:  E£4,794.02
You save:  E£1,438.35
ብር3,989.43
List Price:  ብር5,699.43
You save:  ብር1,710
Kz58,616.62
List Price:  Kz83,741.62
You save:  Kz25,125
CLP$66,326.02
List Price:  CLP$94,755.52
You save:  CLP$28,429.50
CN¥506.51
List Price:  CN¥723.62
You save:  CN¥217.11
RD$4,049.59
List Price:  RD$5,785.38
You save:  RD$1,735.78
DA9,420.19
List Price:  DA13,457.99
You save:  DA4,037.80
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.62
List Price:  Q775.21
You save:  Q232.58
GY$14,613.08
List Price:  GY$20,876.73
You save:  GY$6,263.64
ISK kr9,792.30
List Price:  ISK kr13,989.60
You save:  ISK kr4,197.30
DH706.05
List Price:  DH1,008.69
You save:  DH302.63
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,010.92
List Price:  ден5,730.13
You save:  ден1,719.21
MOP$562.15
List Price:  MOP$803.11
You save:  MOP$240.95
N$1,302.54
List Price:  N$1,860.85
You save:  N$558.31
C$2,571.43
List Price:  C$3,673.63
You save:  C$1,102.20
रु9,317.58
List Price:  रु13,311.40
You save:  रु3,993.82
S/262.81
List Price:  S/375.46
You save:  S/112.65
K268.53
List Price:  K383.63
You save:  K115.10
SAR262.51
List Price:  SAR375.03
You save:  SAR112.52
ZK1,879.71
List Price:  ZK2,685.42
You save:  ZK805.70
L324.19
List Price:  L463.14
You save:  L138.95
Kč1,629.65
List Price:  Kč2,328.17
You save:  Kč698.52
Ft25,373.17
List Price:  Ft36,248.95
You save:  Ft10,875.77
SEK kr758.75
List Price:  SEK kr1,083.98
You save:  SEK kr325.22
ARS$61,468.94
List Price:  ARS$87,816.53
You save:  ARS$26,347.59
Bs482.36
List Price:  Bs689.12
You save:  Bs206.75
COP$272,946.91
List Price:  COP$389,940.87
You save:  COP$116,993.96
₡35,623.88
List Price:  ₡50,893.45
You save:  ₡15,269.56
L1,732.95
List Price:  L2,475.75
You save:  L742.80
₲523,151.84
List Price:  ₲747,391.81
You save:  ₲224,239.96
$U2,683.09
List Price:  $U3,833.15
You save:  $U1,150.06
zł281.85
List Price:  zł402.67
You save:  zł120.81
Already have an account? Log In

Transcript

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

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.