views:

911

answers:

4

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?

+1  A: 

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

Grant Limberg
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.
Julien Chastang
It is not first class. It is just a common design pattern in Cocoa. There is nothing special about it.
robottobor
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").
Kendall Helmstetter Gelner
+4  A: 

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.

Marc Charbonneau
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?
Julien Chastang
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.
Alex Gosselin
+3  A: 

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

finnw
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?
Julien Chastang
+1  A: 

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.

Alex Gosselin