Delegation


#1

Hello,

I just finished this chapter, and i’m still a little fuzzy on how delegation works. This is what i’ve come up with:

Lets say an arm is an instance of a class, hence an object, and so is the brain. The arm, has a delegate property, which it sets to point to the brain. The brain now conforms to a protocol, that lets it know how to handle the delegation on the arm, meaning that it will now know about methods needed to send to the arm to make it do what it wants it to do.

So right now, the arm is basically just an object, and won’t do anything without the brain (it’s delegate) telling it what to do.

I don’t believe this is 100% correct, so if someone could clarify this matter a little more i’d be over joyed. Sorry for using a maybe lame example :stuck_out_tongue:

Thanks on advance
/JBJ


#2

Hi,

Sticking with your analogy I’d turn this around so that the Brain has the delegate property.

So the Brain wants to pick things up but doesn’t have the functionality to do so.

It knows that anything that adopts the protocol can do these things and as an Arm adopts this protocol it instantiates an Arm object, set it’s delegate to point to the Arm and then whenever it needs to pick something up it asks it’s delegate (the Arm) to do so.

The clever bit is that sometime later you decide that you want to pick up a Car.

A Crane object also adopts the protocol and so with a slight change to the Brain it can now pick up Cars.

If this is all a bit weird - you started it :smiley:

HTH
Gareth


#3

Thanks Gareth :slight_smile:

Meh it’s all a little weird still, but i guess with enough practice i’ll get the hang of it eventually :slight_smile: Thanks, your reply helped!

/JBJ