Command Pattern Part 2

9 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

Now in our last video, we have actually created our center app. And we have created these classes inside that, up to this point, our center app is a Windows Forms application. And we do not have anything in our windows form. So let's design a proper UI so that we can send those commands into our receiver actually. So we are going to just go to our views and then I'm going to enable my toolbox section in that and from toolbox, we are going to drag and drop some basic controls into this. I want one label on the top and this label is going to have one button with that with the textbox.

So we're going to have one button and somewhere in between this we are going to put one text box. So it's going to be the it's something like this that this label is actually going to give me a volume from my TV. So we are going to send the volume with this. So it's going to be volume, whatever we type in the textbox, I want to pass that thing on this particular button. So we are specifying the text set volume. And once the volume is set with that, now let's say that we are going to have one more control on the screen which is going to be maybe my group box.

And this group box is going to have another two controls inside that which are allowing me to maybe turn on and turn off my TV. So we have button two and button three in the button to via setting that we are going to make our television on and then maybe on this we are going to make our television off. So we are going to use this thing with those enum which we have actually created. So we have a group box, and maybe the name of the text of the group box we edifying is going to be my power options. And in this power options, we have two buttons On and off. So we are good to go.

We have three buttons, one text box. Now let me find the click event handlers for all three, so that we can go to our form. And we can write the logical code inside this. So we have button one, button two and button three, these three click event handlers are there in our code behind of our form. And now let me just collapse all this fine. So inside this particular form, now we have to write a code.

The first thing which I'm going to do is I want to create one global object on the top for the television. So I have a private television reference variable on the top. And we are going to initialize this exactly below initialize component inside my phone while initializing this television amplifying that this is going to have this IP and this particular port number, which are actually going to call my constructor and he's going to be passed inside that. After this, we have to read the logic inside this three by And clicks but before that I am creating a simple method with the name execute command and you can see that this execute command is actually going to take one parameter of type my commands. And using this method now I can directly execute my commands whichever I am going to pass into this and different different commands which we are going to create we are going to execute with the help of this because internally This is going to connect with a particular instance of the television and on that particular television we are going to execute that command using this execute command method.

Now we are going to pass on different different commands into our button click event handlers. So my button one is actually having a value of the textbox one into the integer format. So it's going to typecast that thing into integer using convert dot n 32. And then the same value of the volume which we are having, we are going to pass on to the volume command and We are actually passing this command into the execute command, which in turn is internally going to assign this thing to our television. Same way, inside button two, we are going to have execute command, which is actually going to make this setting for power on. And the same way.

The second one is going to do for power settings dot power off. Once you're done with this, we actually send three different commands on three different events. Now it's the time to write the logic to receive this command so that we can see whether the communication is happening properly or not. To check this thing, I'm going to my solution explorer, and now it's the time to deal with the receiver app. Inside the receiver app, I'm going to my program.cs file where I have a main method. And inside this main method, we have to make a connection with our center app.

The first thing inside this main method which I'm going to do is I'm going to create one variable With the name IP and then this one is actually going to have the IP addresses with that. So, we are going to add the namespaces which are required for IP address as well as for the TCP listener. And you can see that we need to add system dotnet and system.net dot sockets, which we have already used in the forms application also. So, using these two classes, we declared that we are going to have this IP and this listener. So, you know that this is actually the same thing what we have specified in our vendor application. And this is actually that particular listener property which helps me to connect this receiver app with the sender.

We are going to write some basic logic to connect and to send the command and my logic looks like this. If you check my code, I have a while loop which first time is going to have a true and inside this we are just specifying listener object dot start. The start is a prediction method, which helps me to do the socket communication between those. And then once the start is just going to show me a console dot write line that waiting for the connection. It's something like this, that when the receiver app is going to be on is going to print this line. And that's the time when we have to wait for the connection to the center app.

After this, we are going to create an object of the socket, which is going to accept the socket based on the listener object in which we have the specific IP and port number. We are specifying the client is connected when we are going to have a connection. And with the center app. Then I'm having a variant data which is going to pass some data from this. And using the socket dot receive we are going to receive the data from the sender app. And whatever data we are receiving, we are just going to encode that thing in the proper string from it.

And then we are passing that thing into the variable with the name input. The same input is going to be printed in our console. So that we can see that Which kind of request is going to be sent by the client? Whatever the sender object is going to send, we are going to receive it and we are going to print this input on our console screen. Same way, maybe we want to respond something. So if the response which is coming from the receiver end, we are going to encode that also and we are going to print the response and we are sending the response back to the sender.

So whatever is received, we are just sending the response to the sender that we got the response. And that response is simple. Okay. Once we are done with this, we are even printing that response is successfully sent to the client. And what is the response in that we are good to go with the basic logic like this. Let's run the receiver app first.

So I'm going to right click on my receiver app and I'm going to debug start new testers. When I do this debug start new instance. It will take some time. And you can see that my receiver app is running and which is still waiting for the connection. So I'm keeping this receiver out. aside, and then we are going to do right click debug start new instance for our sender app.

So sender is running receiver is also running. And now let's try to send some code. The first thing is I'm going to click on this power on button. So when I click on this, you can see that it's showing me that we have connected with the client, the client has requested power on, and the response sent to the client is okay, so it's something like that command is successfully sent. Now, let me set some volume, I'm setting that the volume is going to be 20. And then I'm going to click on the button set volume.

This time it's sending the other command which is volume, and in that is passing the parameter 20 with that, okay, let me increase the volume to maybe 50. And now if I set the volume, you can see it's sending me the same command which is volume with the parameter 50. Once we are done with this, we know that our communication is happening successfully. And now let's power off This particular television, when I do that thing you can see this is sending power zero, and earlier it was power one. So this is a same enum, which we are triggering with that. We can again fire some of the other commands by clicking on this button.

But I hope you're getting the scenario of this design pattern which is common pattern. Thank you so much for watching this video. And if you're watching this full series, and if you are liking this thing, please do not forget to rate our video on Udemy 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.