1
00:00:00,240 --> 00:00:06,930
So again in this list then we are going to talk about a very particular design pattern in Iowa that

2
00:00:06,930 --> 00:00:09,080
is called a delegation pattern.

3
00:00:09,300 --> 00:00:15,600
So a delegation pattern is essentially a way for objects to communicate with each other and pass messages

4
00:00:15,600 --> 00:00:24,240
around the way we use it is we use a protocol as a variable for one object to send messages to the other

5
00:00:24,240 --> 00:00:24,830
one.

6
00:00:25,140 --> 00:00:28,600
So with that in mind this go ahead and get us started.

7
00:00:28,620 --> 00:00:35,730
I usually find delegation to be kind of a difficult topic to explain and is a lot easier to get used

8
00:00:35,730 --> 00:00:36,770
to using it.

9
00:00:36,900 --> 00:00:41,690
And it is something that you see literally all over Iowas everything that we do.

10
00:00:41,690 --> 00:00:43,280
We use a delegation pattern.

11
00:00:43,350 --> 00:00:54,960
So let's go to school and start a new project called our fifth project delegation with.

12
00:00:55,080 --> 00:00:58,580
So here is what we want to do with this particular delegation.

13
00:00:58,590 --> 00:01:02,770
I'm going to show you two other examples in the upcoming lessons as well.

14
00:01:02,940 --> 00:01:11,310
But I want to have a class called a user and I want an object from that class to either send or receive

15
00:01:11,340 --> 00:01:16,940
a message to another class that is called let's say friend.

16
00:01:17,040 --> 00:01:22,550
These are the friends of our users in a particular you know chat application.

17
00:01:22,710 --> 00:01:28,680
And to do that I'm going to need a what we call a protocol or any other protocol that one of them has

18
00:01:28,680 --> 00:01:30,110
to use it as a variable.

19
00:01:30,120 --> 00:01:33,960
The other one has to use it as lets call it a receiver.

20
00:01:34,170 --> 00:01:36,120
So let's get started with that.

21
00:01:36,120 --> 00:01:41,900
So first I'm going to do the easy parts of this application I'm learned out and use Wi-Fi.

22
00:01:42,630 --> 00:01:47,130
And you don't really have to either understand this or the code is a little bit cleaner and easier to

23
00:01:47,130 --> 00:01:48,060
see.

24
00:01:48,090 --> 00:01:50,740
I'm going to call this one user.

25
00:01:51,180 --> 00:01:55,260
And obviously I'm going to add another one called Franzia.

26
00:01:55,380 --> 00:02:02,880
As for the French class I'm going on another one and I'm going to call this fun protocol.

27
00:02:03,040 --> 00:02:05,350
So let's see what we have in either of these.

28
00:02:05,350 --> 00:02:13,030
The first thing I need in the user I have a class called the user with no superclass no base class and

29
00:02:13,020 --> 00:02:16,180
it has one variable called the name.

30
00:02:16,510 --> 00:02:17,700
And that's a string.

31
00:02:17,980 --> 00:02:21,020
And that's about that's what every user has a name.

32
00:02:21,160 --> 00:02:24,300
And because they have a name they also get to have that in it.

33
00:02:24,310 --> 00:02:31,040
So I'm going to say any name is going to be a string or something they've done hundreds of times already.

34
00:02:31,190 --> 00:02:33,770
Self-taught the name becomes input's name.

35
00:02:33,910 --> 00:02:39,730
That's a lot that I'm going to copy this whole thing because my friend Claus is going to be very similar

36
00:02:39,730 --> 00:02:43,380
to that and to say Dad is friends.

37
00:02:43,390 --> 00:02:46,990
And again every friend has a name that hasn't in it.

38
00:02:46,990 --> 00:02:53,710
My protocol is going to have a protocol as you guessed it Protocol my protocol is going to be called

39
00:02:53,990 --> 00:02:55,850
to call it something particular.

40
00:02:55,850 --> 00:02:58,060
But you could essentially call it anything.

41
00:02:58,060 --> 00:03:04,150
The only thing that is very often you know a common practice in iOS is you call it whatever you want

42
00:03:04,480 --> 00:03:08,010
and you finish with delegate even though you don't have to.

43
00:03:08,020 --> 00:03:09,820
But it's a common practice.

44
00:03:09,820 --> 00:03:13,270
Somebody call this protocol sending message delegate.

45
00:03:13,270 --> 00:03:18,970
So it's a delegate protocol that is going to send the messages and it's got only one function and it's

46
00:03:18,970 --> 00:03:21,770
called send the message.

47
00:03:22,250 --> 00:03:28,200
And it has a message that is a string a string.

48
00:03:28,420 --> 00:03:29,910
So that's about that.

49
00:03:29,920 --> 00:03:38,740
I've got a User class I have a friend class I have a protocol that they each have the things that we

50
00:03:38,740 --> 00:03:39,760
know they have with me.

51
00:03:40,150 --> 00:03:49,460
So let's say this is our plan our plan is for our users to send a message to a friend.

52
00:03:49,540 --> 00:03:54,760
So we're going to have an object of the type of user they're going to have another object of the type

53
00:03:54,760 --> 00:03:57,300
friend and we're going to send a message to that friend.

54
00:03:57,520 --> 00:04:03,060
Obviously you know in an ideal world of ordinary life application there would be a two way communication.

55
00:04:03,070 --> 00:04:05,840
But for now you're only going to worry about one side of this.

56
00:04:05,860 --> 00:04:11,740
And this is essentially just to get a sense of how a delegation pattern essentially works.

57
00:04:11,770 --> 00:04:18,340
Now since we set our user is going to send a message to a friend I'm going to go to the French class

58
00:04:18,370 --> 00:04:27,130
and say the French class is actually conforming to my protocol which is sending the message delegate.

59
00:04:27,130 --> 00:04:33,630
And if you remember from our previous lesson we now have to define that particular method.

60
00:04:33,630 --> 00:04:36,200
I think we call it send a message right.

61
00:04:36,220 --> 00:04:43,390
So I'm going to say that and that means friends now has a built in function that sends a message and

62
00:04:43,510 --> 00:04:48,650
it runs within the body of anyone that is an object of the type friend.

63
00:04:48,820 --> 00:04:55,770
I'm going to say whenever this happens you do this say print on the printer I'm going to say well this

64
00:04:55,780 --> 00:05:01,010
first prints the name of this particular object I'm going to say name.

65
00:05:01,200 --> 00:05:05,260
And then I'm going to say has received what has it received.

66
00:05:05,260 --> 00:05:09,410
It has received the message whether it is them or us.

67
00:05:09,850 --> 00:05:13,540
So so far we haven't done anything that's different than in the past.

68
00:05:13,570 --> 00:05:20,170
We have conformed a class to a certain protocol and that protocol has a function and that function is

69
00:05:20,170 --> 00:05:22,160
happening in a certain way.

70
00:05:22,180 --> 00:05:29,610
The next thing we have to do is to add a delegate of the same type as a variable to our user.

71
00:05:29,830 --> 00:05:31,530
So let's see how we are going to do that.

72
00:05:31,540 --> 00:05:35,470
I'm going to say I have a value bet and we usually call it delegates.

73
00:05:35,470 --> 00:05:36,070
All right.

74
00:05:36,280 --> 00:05:40,420
And that is of the types in the end message delegate.

75
00:05:40,630 --> 00:05:42,970
So let's see what this actually means.

76
00:05:42,970 --> 00:05:50,410
This simply means that I not object that is called delegate is becoming an attribute for the user objects

77
00:05:50,800 --> 00:05:53,790
and is of the type of sending message delegates.

78
00:05:53,800 --> 00:06:02,020
This is very similar to saying that my string is going to be of the type and as a string if you're simply

79
00:06:02,020 --> 00:06:05,430
using god type to introduce a new one.

80
00:06:05,470 --> 00:06:12,510
Now since we are doing that we obviously can do things such as in the case of a string saying my a string.

81
00:06:12,550 --> 00:06:19,660
Append a string or in the case of the delegate saying delegate to dot and calling that method from our

82
00:06:19,660 --> 00:06:21,350
delegate which is send a message.

83
00:06:21,360 --> 00:06:23,910
And that's exactly what we want to do.

84
00:06:24,100 --> 00:06:28,180
Now bear with me for a minute let me finish this and see to the finish line.

85
00:06:28,210 --> 00:06:31,820
And then I come back in Bibi-Ri recap everything one more time.

86
00:06:31,960 --> 00:06:33,630
So let's go ahead and use this.

87
00:06:33,640 --> 00:06:38,500
I'm going to go to my main and in the main I'm going to say well we want to have a usery when I have

88
00:06:38,500 --> 00:06:40,700
a friend and we want to send a message.

89
00:06:40,750 --> 00:06:42,140
So let's have a friend first.

90
00:06:42,150 --> 00:06:46,040
I'm going to say well let's call this one Jack.

91
00:06:46,210 --> 00:06:46,600
All right.

92
00:06:46,600 --> 00:06:52,610
And Jack is going to be a friend with the name and that name is Jack.

93
00:06:52,630 --> 00:06:53,790
Simple as that.

94
00:06:53,800 --> 00:07:01,000
I'm going to have my very own use that I'm going to save my user that's going to be a user with that

95
00:07:01,000 --> 00:07:01,790
name.

96
00:07:01,930 --> 00:07:04,280
In this case I'm put mining I mean.

97
00:07:04,470 --> 00:07:10,740
And now we have something very interesting happening that is there has something called delegate so

98
00:07:10,740 --> 00:07:16,670
I can actually use that and it has to be something of this type.

99
00:07:16,710 --> 00:07:23,640
So the only one that we know of this type on this we want to initialize a brand new one are the objects

100
00:07:23,640 --> 00:07:24,960
of the type friends.

101
00:07:24,990 --> 00:07:32,430
Once again the only thing that we can make delegate assigned to is any one of this type that type sends

102
00:07:32,430 --> 00:07:33,610
a message delegate.

103
00:07:33,780 --> 00:07:36,840
And this is the only class that conforms to that.

104
00:07:36,840 --> 00:07:44,040
So I'm going to go to Maine and I'm to say for my user that delegate is going to become.

105
00:07:44,160 --> 00:07:50,010
And you see them when they go there it says you have to give me a sending message delegate or someone

106
00:07:50,040 --> 00:07:51,770
that conforms to that.

107
00:07:51,860 --> 00:07:55,600
The only one in here that conforms to that is actually Jack.

108
00:07:55,980 --> 00:08:03,900
So now the only state that is left is that know the tasks of this sky through the delegation are being

109
00:08:03,900 --> 00:08:05,630
sent to jail.

110
00:08:05,910 --> 00:08:12,100
And because of that Jack will receive that one and do something with it in here.

111
00:08:12,270 --> 00:08:15,680
So the only thing we have to do is we have to call this function.

112
00:08:15,780 --> 00:08:23,700
I'm going to go in here and say well now that I know who it's being delegated to my user that delegate

113
00:08:23,850 --> 00:08:34,670
do not send the message and the message is let's say hi from the from my name so my user darte name.

114
00:08:34,680 --> 00:08:36,300
So let's see what happens in here.

115
00:08:36,330 --> 00:08:42,210
And then I'm going to recap everything and then we're going to do two more two more exercises similar

116
00:08:42,210 --> 00:08:46,760
to this that was an optional value I had to force under it.

117
00:08:46,770 --> 00:08:50,200
So let's run this application and the moment it runs.

118
00:08:50,220 --> 00:08:51,680
If it all goes well.

119
00:08:52,610 --> 00:08:53,640
We will see.

120
00:08:53,800 --> 00:09:02,620
Jack has received the hi from me and Jack is this object friend which has a function called send the

121
00:09:02,620 --> 00:09:06,070
message which is this guy.

122
00:09:06,100 --> 00:09:08,400
This is the member of the protocol.

123
00:09:08,470 --> 00:09:15,520
So the only time that this thing gets called is if a delegate or a protocol delegate calls it.

124
00:09:15,690 --> 00:09:24,400
And in here I have said well this guy my user which has a variable called delegate which has delegated

125
00:09:24,460 --> 00:09:30,070
is job to Jack is sending a message and that message is this thing.

126
00:09:30,400 --> 00:09:35,760
So begin to move on to our next lesson which I'm actually going to do one more of these and I'm going

127
00:09:35,770 --> 00:09:37,400
to really explain everything.

128
00:09:37,700 --> 00:09:42,730
And I'm perhaps a little bit you know an easier way to see them because this was more of the concept.

129
00:09:42,730 --> 00:09:48,010
Now we're going to see it in some sort of a practice so I'm going to pause here for a second and then

130
00:09:48,070 --> 00:09:51,170
I'm going to move into the next lesson.

131
00:09:51,200 --> 00:09:52,220
So I see the.
