1
00:00:00,610 --> 00:00:06,170
You guys the next pattern we're going to look at is another behavioral pattern like the observer.

2
00:00:06,240 --> 00:00:12,240
It's called the Mediator pattern and the idea is to have a mediator which is basically an interface

3
00:00:12,240 --> 00:00:17,700
for communicating with what are called colleagues which are just mediated objects.

4
00:00:17,820 --> 00:00:21,250
And I think one of the best examples of this is a chat room.

5
00:00:21,270 --> 00:00:26,850
Now this won't be a full project but if you want to implement this you know along with something like

6
00:00:26,850 --> 00:00:33,900
socket I O you could do that but let's go ahead and just create a very basic example that kind of shows

7
00:00:33,900 --> 00:00:36,980
you how the Mediator Pattern is put together.

8
00:00:37,620 --> 00:00:42,460
So we're going to have to construct functions one for the user one for the chat room.

9
00:00:42,450 --> 00:00:52,290
So let's say Konst user set that to a function and each user will have a name.

10
00:00:52,410 --> 00:00:57,000
So we want to set this on name equal to name.

11
00:00:57,150 --> 00:01:03,410
And then we're also going to set this chat room and we're going to initially set that to now.

12
00:01:03,840 --> 00:01:10,560
OK and then we'll also have a constructor function for the chat room.

13
00:01:10,860 --> 00:01:14,390
So we'll set that to function.

14
00:01:15,960 --> 00:01:21,150
And that's not going to take anything in and it's just going to have a property called users which will

15
00:01:21,150 --> 00:01:25,860
be set to just an empty object to start with.

16
00:01:25,860 --> 00:01:29,270
All right so this will be a list of users.

17
00:01:29,340 --> 00:01:33,280
So let's go to the user constructor first.

18
00:01:33,360 --> 00:01:35,660
And actually we're going to create a prototype here.

19
00:01:35,670 --> 00:01:40,010
So it's a user Dortch prototype.

20
00:01:40,170 --> 00:01:43,820
And by the way if you guys want to use ESX classes that's fine.

21
00:01:43,830 --> 00:01:51,540
I think in my opinion you should get used to writing object oriented code like this because this is

22
00:01:51,540 --> 00:02:00,650
what is actually being this is what is actually in the javascript classes are just sugar over this.

23
00:02:00,870 --> 00:02:06,330
Of course you can you know you can you you can write classes but I would suggest when you're practicing

24
00:02:06,390 --> 00:02:09,450
I would suggest doing it this way but it's up to you.

25
00:02:09,450 --> 00:02:12,480
You know you guys can write this as a class if you want.

26
00:02:12,720 --> 00:02:16,710
So and I've done a couple of examples where you can see how to do that.

27
00:02:16,710 --> 00:02:21,780
I mean it's pretty simple you just create your constructor in your class put your properties and then

28
00:02:22,050 --> 00:02:26,980
each of these prototype methods will be an actual class method.

29
00:02:27,000 --> 00:02:29,410
So in here let's do a send.

30
00:02:29,590 --> 00:02:31,470
OK we're going to have a send function

31
00:02:35,640 --> 00:02:41,360
and we're going to have a receive function.

32
00:02:42,180 --> 00:02:49,230
And the idea is to be able to have users that can send messages to another single user or to broadcast

33
00:02:49,230 --> 00:02:51,050
to the entire chat room.

34
00:02:51,050 --> 00:02:51,360
All right.

35
00:02:51,360 --> 00:02:59,570
And again the chat the chat room is the mediator and then the users of the colleagues of the chat room.

36
00:02:59,700 --> 00:03:06,840
So the center function will take in any message and a to sort of be wherever the messages and then the

37
00:03:06,840 --> 00:03:07,790
user.

38
00:03:08,020 --> 00:03:09,280
The to use.

39
00:03:09,600 --> 00:03:14,160
And this will actually call from the chat room.

40
00:03:14,160 --> 00:03:18,050
So say this chat room door send.

41
00:03:18,090 --> 00:03:23,550
Chat Room will also have a send and that will pass in the message.

42
00:03:23,590 --> 00:03:28,030
The two I'm sorry this message is this.

43
00:03:28,050 --> 00:03:34,080
And then two this is pertaining to the user.

44
00:03:34,080 --> 00:03:35,600
All right so let's see.

45
00:03:35,610 --> 00:03:42,280
After that we want to do the receive and the receive is going to take in a message.

46
00:03:42,330 --> 00:03:44,470
It's going to take in a from.

47
00:03:44,790 --> 00:03:45,210
OK.

48
00:03:45,210 --> 00:03:52,090
And then all we want to do when it's received is Konsole dot log and we'll put some back ticks in here

49
00:03:52,090 --> 00:03:54,150
and we're going to put the name.

50
00:03:54,210 --> 00:04:00,450
So we're going to say from DOT name from DOT name to

51
00:04:03,220 --> 00:04:04,230
the current user.

52
00:04:04,230 --> 00:04:06,630
So this Doughton name

53
00:04:09,700 --> 00:04:13,890
and then we want to put the message.

54
00:04:14,950 --> 00:04:15,690
All right.

55
00:04:15,870 --> 00:04:17,270
And that's it for the user.

56
00:04:17,430 --> 00:04:25,750
So now the chat room we're going to go down in and we're going to say return

57
00:04:28,420 --> 00:04:34,240
and here we're going to first have a register because we need users to register with the chat room.

58
00:04:34,360 --> 00:04:37,600
OK the colleagues have to register with the mediator.

59
00:04:37,810 --> 00:04:44,500
So we'll have this as a function that will take in the actual user.

60
00:04:45,370 --> 00:04:45,860
OK.

61
00:04:45,880 --> 00:04:54,550
And then we're going to set users and then as a key We're going to use user dog name.

62
00:04:54,550 --> 00:04:54,930
All right.

63
00:04:54,930 --> 00:05:00,320
So user name and we're going to set that to the user that's being passed in.

64
00:05:01,090 --> 00:05:10,480
And then we just want to set user dot chat room to this day to get to the current chat room and if we

65
00:05:10,480 --> 00:05:16,600
look in the user construct or this chat room it is now by default but now we're setting it to the current

66
00:05:16,600 --> 00:05:18,340
chat room.

67
00:05:18,400 --> 00:05:23,820
Now the chat is also going to have a sense that's what we're doing right here is seeing this chat room

68
00:05:23,820 --> 00:05:24,890
Dotts send.

69
00:05:25,060 --> 00:05:27,560
So we have to add that to the register

70
00:05:34,150 --> 00:05:42,370
and this takes in as you can see here message this which is actually going to be from user and then

71
00:05:42,370 --> 00:05:53,460
to the two years or so here we're going to see a message and from end to.

72
00:05:53,650 --> 00:05:53,990
All right.

73
00:05:54,000 --> 00:05:59,540
Now here's where we need to decide if the message is going to a certain use or if it's going to be broadcasted

74
00:05:59,540 --> 00:06:00,680
to the entire run.

75
00:06:00,680 --> 00:06:05,100
So we're going to check to see if there's a true value.

76
00:06:05,360 --> 00:06:07,130
So we'll say if.

77
00:06:07,550 --> 00:06:20,740
And let's put in place here so if to that is a single user message so will say to dog or see you've

78
00:06:22,180 --> 00:06:31,790
never going to pass the message and then from value else then we're actually going to loop through all

79
00:06:31,790 --> 00:06:33,770
of the users and do the receive.

80
00:06:33,770 --> 00:06:41,460
So let's do a for loop and we'll just say this is a mouse message.

81
00:06:41,540 --> 00:06:42,890
So we'll save for

82
00:06:45,590 --> 00:06:56,610
let's say for key users and then we're going to check in to do an if statement here in me and we're

83
00:06:56,630 --> 00:07:02,990
going to say if users key we want to make sure that it's not the act of the user that's sending it so

84
00:07:03,020 --> 00:07:15,980
if it's not equal to the from user then we want to call users and key and then do our receive.

85
00:07:16,000 --> 00:07:16,480
All right.

86
00:07:16,500 --> 00:07:21,800
And Percival taken message in from just like it did right here.

87
00:07:21,830 --> 00:07:22,220
All right.

88
00:07:22,220 --> 00:07:24,090
So that should do it.

89
00:07:24,110 --> 00:07:27,310
So that gives us our interface Let's go ahead and save it.

90
00:07:27,410 --> 00:07:32,180
Now down here is where we're going to use our pattern here.

91
00:07:32,180 --> 00:07:40,700
So we're going to create a couple of users so let's say conc Brad cools and we'll say new user and that

92
00:07:40,700 --> 00:07:41,660
takes in a name

93
00:07:44,450 --> 00:07:46,710
we'll do a couple.

94
00:07:47,420 --> 00:07:55,940
Let's say Brad will say Jeff and let's say Sarah

95
00:08:01,540 --> 00:08:02,950
so that will create some users.

96
00:08:02,950 --> 00:08:10,420
Now we want to create the chat room the mediator So let's say a chat room equals new

97
00:08:14,200 --> 00:08:17,140
do a chat room like that

98
00:08:20,220 --> 00:08:23,030
and then down here we want to register the users.

99
00:08:23,080 --> 00:08:32,040
So it's a chat room keep it in uppercase our chat room door register know we want to register.

100
00:08:32,040 --> 00:08:32,720
Brad

101
00:08:38,680 --> 00:08:39,990
it's registered.

102
00:08:40,000 --> 00:08:45,250
Jeff and all registers.

103
00:08:45,710 --> 00:08:49,160
OK so now that everyone is registered we should be able to send messages.

104
00:08:49,160 --> 00:08:53,720
So let's say Brad Daunt send.

105
00:08:53,990 --> 00:08:58,150
Let's say hello Jeff.

106
00:08:58,310 --> 00:09:02,750
And then the second parameter is going to be the user I want to send to which is Jeff.

107
00:09:02,750 --> 00:09:05,560
So let's go ahead and save that we get Brad to G.F..

108
00:09:05,750 --> 00:09:07,320
Hello Jeff.

109
00:09:07,910 --> 00:09:16,740
So let's say that Sarah wants to send a message to Brad.

110
00:09:17,900 --> 00:09:18,830
Hello Brad.

111
00:09:18,830 --> 00:09:27,830
You are the best dev ever.

112
00:09:27,970 --> 00:09:31,290
I can't even spell his own name.

113
00:09:31,910 --> 00:09:33,090
So Sarid a brad.

114
00:09:33,110 --> 00:09:33,770
Hello Brad.

115
00:09:33,770 --> 00:09:35,550
You were the best ever.

116
00:09:35,570 --> 00:09:47,100
Now let's say that Jeff wants to send the message to the whole room so he'll just say hello everyone.

117
00:09:49,250 --> 00:09:51,260
And say when you can see Jeff too Brad.

118
00:09:51,260 --> 00:09:52,820
Hello everyone Jeff to Sarah.

119
00:09:52,820 --> 00:09:53,890
Hello everyone.

120
00:09:53,900 --> 00:10:00,620
He doesn't say it to himself because here we check to see we made sure that it wasn't the from user

121
00:10:00,650 --> 00:10:02,580
that was going to send the message.

122
00:10:02,830 --> 00:10:03,220
All right.

123
00:10:03,230 --> 00:10:05,250
Or received the message I should say.

124
00:10:05,810 --> 00:10:10,290
OK so that I think is a pretty good example of the Mediator pattern.

125
00:10:10,460 --> 00:10:13,970
Hopefully that made sense to you and I will see you in the next video.

