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?
Delegation is an object oriented design pattern. An example in Java is on Wikipedia: Delegation Pattern
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.
java.lang.reflect.Proxy
is the closest equivalent in java. It's tedious to use though.
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.