Objective-C Delegation Explained to a Java Programmer
Asked Answered
P

4

9

I am fairly new to Objective-C, but experienced in Java. Is there the equivalent concept of Objective-C "delegation" in Java, so that I may understand this notion better? Would there be a way to emulate the delegation concept in Java?

Pointdevice answered 14/1, 2009 at 20:35 Comment(0)
C
9

Delegation is just a way to avoid having to subclass an object like a table view to implement application specific behavior, and instead put that responsibility on the controller. When you create the table view you assign it a controller object, which implements a defined set of methods (some may be required, others may be optional). When the table view needs data or has to make a decision on how to display itself, it asks the delegate if it implements the appropriate method, and calls it if it does to make its decision.

Carlisle answered 14/1, 2009 at 21:13 Comment(2)
Thanks. In your example, you are providing a delegate for the controller. That delegate wraps the controller which the table view will interact with. Correct? In Java you would have the delegate implement the same interface as the regular controller. Is there this notion in Objective-C?Pointdevice
I'd say not exactly, it doesn't wrap the controller, that would be more along the lines of a subclass. What the delegate does is answer the questions about what is different from one use of the tableViewController object to another, i.e. the tableViewController would not always have the same amount of cells, so the delegate would have a required method -numberOfCells which the controller would call to determine how many cells there were, then it would have something like -cellAtIndex:(int)index to ask the delegate for each of the cells. The trick here is that the table view isn't subclassed.Epigenous
E
4

Here's a way to think of a delegate - in the OOP typical example, I have a car object. I don't want to ever subclass it again, I just want to use it as is, so how do I make it act like a chevy or a mustang? I give it a delegate.

My car would have methods to drive, methods to honk, etc.

My delegate would have methods like "what's my top speed" and "what does the horn sounds like" and "are my windows tinted"

So when I call -drive on my car object, (which isn't subclassed) that method calls my delegate's topSpeed method, and the delegate tells it 120mph, so the car knows how fast it should go without having to be a mustang.

in Objective C there's usually a protocol that defines what a delegate should respond to, i.e. for my car object's delegate there would be a protocol declared like this:

 @protocol carDelegate

 -(int)carTopSpeed;
 -(UIColor*)carColor;
 -(BodyShape*)carBodyShape;
 -(DragCoefficient*)carDragCoefficient;
 -(HoodOrnament*)carHoodOrnament     

 @optional
 -(BOOL)windowsTinted;

 @end

Then you can make your own object that conforms to this protocol (implements all the required methods, and any optional ones that are seen as needed)

And the car object would expect an (id) to be passed to it as a delegate.

Then the car object has managed to avoid being subclassed, and still can behave according to the user's needs.

Epigenous answered 20/10, 2010 at 22:57 Comment(2)
Why would -drive call -topSpeed? I don't see the connectionBloodstone
Assume the Car object is a bit like an actor, and carDelegate is a bit like the director. Rather than subclass the actor, we just make the actor ask the director how to act, and you create custom directors. So when you ask the car to drive, the car asks the delegate how fast it should drive.Epigenous
E
3

java.lang.reflect.Proxy is the closest equivalent in java. It's tedious to use though.

Elephantiasis answered 14/1, 2009 at 21:18 Comment(1)
java.lang.reflect.Proxy is for creating Java proxies/wrappers/delgates at runtime. Is this true for Objective-C delegates? They are a runtime and not compile time concept?Pointdevice
R
2

Delegation is an object oriented design pattern. An example in Java is on Wikipedia: Delegation Pattern

Racemose answered 14/1, 2009 at 20:46 Comment(3)
Thanks for the link. Is it really as simple as wrapping one object with another -- and exposing those same methods, but with (slightly) different behavior? My (very limited) understanding of delegation is that it is much more of a first class concept in Objective-C.Pointdevice
It is not first class. It is just a common design pattern in Cocoa. There is nothing special about it.Hypochromia
It's a touch easier to do in Objective C, as it's easier to detect if an arbitrary object has a method you want to call (instead of method it's really called a "selector") which enables easy use of optional methods by a contained object (the wrapped objects are generally termed "delegates").Sate

© 2022 - 2024 — McMap. All rights reserved.