So will come back again in this list and we're going to talk about protocols so protocols as the name
implies are essentially a set of instructions and a class or a structure on ENM will adopt to that protocol
meaning whatever rules are there in the protocol that class or data structure that Inform should follow.
So why is this useful for us.
It is useful for us in a situation where we have different classes that we want all of them to conform
to a certain set of things.
For instance they all have showed a certain number of primitives or they all should implement a certain
function.
Now what is important to understand about protocols is that they don't actually contain any logic.
They just have the declaration of the methods or attributes they don't understand what is going to happen
to be taught.
And they don't have the implementation of it.
So in Turin you could have protocols that are adopted by different classes but each class does its own
thing on their mettle.
So the school head into a school and going I start a new project I'm going to make it of the types with
and is going to be protocol protocols with particular protocols the right protocol swift
animal protocols.
If I'm going to do this I'm going to say I have one protocol and that protocol is going to be let's
call our protocol admissible.
All right.
It means anyone who conforms to this is acceptable or is admissible not nine.
And we are working on that same application that we have discussed multiple times which is about a real
estate property listing application.
So in the application we have a clause for property.
We have a class for Agent.
These are the people who list the different properties and we have in class for the user.
These other people who browse between properties.
Now all of these they share some attributes.
Let's say they all have a status that is called active whether they are currently active or not.
It is the property that is currently active or not an agent does.
I will not use it as I will not.
They will have a rating.
So each of them at any given point has a rating user agent or property.
Also they all have a function called rates.
It's of course they all have a you idea they have a unique ID.
So what they want to do is this.
I want to make sure that this protocol includes all of these and every class that conforms to this protocol
makes sure those classes have to incorporate these items in them.
So the first thing is I'm going to say there is a valuable called active that is of the type Boolean
and this is something that we can both get unset.
This is essentially like that thing we had the read only or read the right and we'll get to see more
of these things soon enough that there are items that we can or attributes that we can only read them
or the ones that we can both read and write into them.
So once we have that I have another value does of it is called you.
ID is a string.
And again it is a set and get we can both set and get them.
We have something else called larger rate which is a float and this event is only.
Yet we can not set it simply because rating should be calculated based on some some sort of algorithm
something that calculates the value and tells us what the current weight rating is 4.2 or something
to that effect.
And then finally we have a function call rate it and this function doesn't have any body weight.
Or maybe it has only one argument to rate it with import rate of a floater value and then like I said
it doesn't have any logic.
It just says this declaration exists.
And that's the goal of having a protocol.
Now let's go ahead and make our archery classes.
I'm going to say I have a class.
This course is going to be called property and it does conform to admissible.
And the moment we do this we should see some sort of error given to us by the compiler OFX code.
It's saying that it doesn't completely conform to this protocol you are missing some stuff.
Would you like to add them I'm going to say yes and then it says well if you want to add them you have
to make sure you have the activity you have the you ID you have the rating and you have this function
and now that we have those we have to actually initialize this.
We can just leave them there because the class is going to say well I know you have these three but
when are you going are going to initialize them.
I'm going to say in that and in the in it I'm going to say I haven't been active.
That is half the time.
And so when the initialise something is it active or not and then that to you idea and rate they have
to be calculated at some other time.
In that case active becomes Impe active.
And then I'm going to say you ID for instance becomes you you ID dot you you idea string.
And then the rate at the very very beginning we set it to be zero.
And that whole my property class has conformed to the admissible protocol or Essentially it's been adapted
to that and that's the same thing we have to do with everybody else.
If we have a class called Agent we have to do that if you have a classical user you have to that.
And of course each of these classes will have their own thing applying a protocol is very similar to
writing three classes are making sure that each of the three classes have these.
Even if it didn't have the protocol and we didn't have that this class would work on its own nothing
is wrong with it having the political makes sure that we apply the same rule to everyone.
I'm one of the places that we use protocols.
Is isn't something that we call it delegation pattern.
And that's something that you're going to see in an upcoming lesson which is a very very important part
of you know Iowa's development.
But for now Protocols are a set of rules.
They have their instructions no logic they have the attributes and they know where they are get upset
or only get.
And then when we adopt them by any class it means all of those things in that protocol must exist in
here.
That function has to be there and those because they have to be there.
And with that let's move on to our.