So again in this list then we are going to talk about a very particular design pattern in Iowa that
is called a delegation pattern.
So a delegation pattern is essentially a way for objects to communicate with each other and pass messages
around the way we use it is we use a protocol as a variable for one object to send messages to the other
one.
So with that in mind this go ahead and get us started.
I usually find delegation to be kind of a difficult topic to explain and is a lot easier to get used
to using it.
And it is something that you see literally all over Iowas everything that we do.
We use a delegation pattern.
So let's go to school and start a new project called our fifth project delegation with.
So here is what we want to do with this particular delegation.
I'm going to show you two other examples in the upcoming lessons as well.
But I want to have a class called a user and I want an object from that class to either send or receive
a message to another class that is called let's say friend.
These are the friends of our users in a particular you know chat application.
And to do that I'm going to need a what we call a protocol or any other protocol that one of them has
to use it as a variable.
The other one has to use it as lets call it a receiver.
So let's get started with that.
So first I'm going to do the easy parts of this application I'm learned out and use Wi-Fi.
And you don't really have to either understand this or the code is a little bit cleaner and easier to
see.
I'm going to call this one user.
And obviously I'm going to add another one called Franzia.
As for the French class I'm going on another one and I'm going to call this fun protocol.
So let's see what we have in either of these.
The first thing I need in the user I have a class called the user with no superclass no base class and
it has one variable called the name.
And that's a string.
And that's about that's what every user has a name.
And because they have a name they also get to have that in it.
So I'm going to say any name is going to be a string or something they've done hundreds of times already.
Self-taught the name becomes input's name.
That's a lot that I'm going to copy this whole thing because my friend Claus is going to be very similar
to that and to say Dad is friends.
And again every friend has a name that hasn't in it.
My protocol is going to have a protocol as you guessed it Protocol my protocol is going to be called
to call it something particular.
But you could essentially call it anything.
The only thing that is very often you know a common practice in iOS is you call it whatever you want
and you finish with delegate even though you don't have to.
But it's a common practice.
Somebody call this protocol sending message delegate.
So it's a delegate protocol that is going to send the messages and it's got only one function and it's
called send the message.
And it has a message that is a string a string.
So that's about that.
I've got a User class I have a friend class I have a protocol that they each have the things that we
know they have with me.
So let's say this is our plan our plan is for our users to send a message to a friend.
So we're going to have an object of the type of user they're going to have another object of the type
friend and we're going to send a message to that friend.
Obviously you know in an ideal world of ordinary life application there would be a two way communication.
But for now you're only going to worry about one side of this.
And this is essentially just to get a sense of how a delegation pattern essentially works.
Now since we set our user is going to send a message to a friend I'm going to go to the French class
and say the French class is actually conforming to my protocol which is sending the message delegate.
And if you remember from our previous lesson we now have to define that particular method.
I think we call it send a message right.
So I'm going to say that and that means friends now has a built in function that sends a message and
it runs within the body of anyone that is an object of the type friend.
I'm going to say whenever this happens you do this say print on the printer I'm going to say well this
first prints the name of this particular object I'm going to say name.
And then I'm going to say has received what has it received.
It has received the message whether it is them or us.
So so far we haven't done anything that's different than in the past.
We have conformed a class to a certain protocol and that protocol has a function and that function is
happening in a certain way.
The next thing we have to do is to add a delegate of the same type as a variable to our user.
So let's see how we are going to do that.
I'm going to say I have a value bet and we usually call it delegates.
All right.
And that is of the types in the end message delegate.
So let's see what this actually means.
This simply means that I not object that is called delegate is becoming an attribute for the user objects
and is of the type of sending message delegates.
This is very similar to saying that my string is going to be of the type and as a string if you're simply
using god type to introduce a new one.
Now since we are doing that we obviously can do things such as in the case of a string saying my a string.
Append a string or in the case of the delegate saying delegate to dot and calling that method from our
delegate which is send a message.
And that's exactly what we want to do.
Now bear with me for a minute let me finish this and see to the finish line.
And then I come back in Bibi-Ri recap everything one more time.
So let's go ahead and use this.
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
a friend and we want to send a message.
So let's have a friend first.
I'm going to say well let's call this one Jack.
All right.
And Jack is going to be a friend with the name and that name is Jack.
Simple as that.
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
name.
In this case I'm put mining I mean.
And now we have something very interesting happening that is there has something called delegate so
I can actually use that and it has to be something of this type.
So the only one that we know of this type on this we want to initialize a brand new one are the objects
of the type friends.
Once again the only thing that we can make delegate assigned to is any one of this type that type sends
a message delegate.
And this is the only class that conforms to that.
So I'm going to go to Maine and I'm to say for my user that delegate is going to become.
And you see them when they go there it says you have to give me a sending message delegate or someone
that conforms to that.
The only one in here that conforms to that is actually Jack.
So now the only state that is left is that know the tasks of this sky through the delegation are being
sent to jail.
And because of that Jack will receive that one and do something with it in here.
So the only thing we have to do is we have to call this function.
I'm going to go in here and say well now that I know who it's being delegated to my user that delegate
do not send the message and the message is let's say hi from the from my name so my user darte name.
So let's see what happens in here.
And then I'm going to recap everything and then we're going to do two more two more exercises similar
to this that was an optional value I had to force under it.
So let's run this application and the moment it runs.
If it all goes well.
We will see.
Jack has received the hi from me and Jack is this object friend which has a function called send the
message which is this guy.
This is the member of the protocol.
So the only time that this thing gets called is if a delegate or a protocol delegate calls it.
And in here I have said well this guy my user which has a variable called delegate which has delegated
is job to Jack is sending a message and that message is this thing.
So begin to move on to our next lesson which I'm actually going to do one more of these and I'm going
to really explain everything.
And I'm perhaps a little bit you know an easier way to see them because this was more of the concept.
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
I'm going to move into the next lesson.
So I see the.