It balances out the .NET news. Dynamic Delegation is a little known feature of Java that gives it the power to do some very flexible things (sort of like Smalltalk’s “cannotunderstand” message handler). Without it, MacOSX’s Java API to their Objective-C API might not be possible without a lot of ugly kludges.
Personnally, I wouldn’t mind hearing more OS-related news about Python (Linux’s current Visual Basic) and Objective-C (MacOSX’s API and the GNUstep API).
@Anonymous: Where you’d use this feature is if you wanted to write some code to wrap method calls on a class or something implementing an interface. E.g. you want to intercept method call X to this class and do something else before you call the method.
So to everywhere that uses this class, you pass the proxy that you’ve created instead of the real instance.
The “delegation pattern” really just means to wrap a method from another class, using the same name and no additional code.
in C++:
class A::do_work(){ /* do actual work */ }
class B::do_work(){ m_a.do_work(); } // delegate work to A
Why would you use this?
1. If you’ve got a bodged design where your high level classes cannot be used to satisfy their functional requirements. Solution: make a delegation class, (or spaghetti class) that wraps a bunch of functionality from other classes.
2. If you’ve got a bunch of low level simple classes that are functionally atomic (or have a bunch of static functions) and you want to pull them all together into one super class.
3. If your object oriented language does not have multiple inheretance, simply wrap mutiple classes using “delegation”.
I consider this an anti-pattern because it works against functionally orthogonal classes.
Delegation is also used to enhance basic classes at runtime with extra features. Like dynamic persistence (ala Hibernate)
This is both dumb and clever.
It’s dumb because it becomes impossible for the programmer to know what’s happening unless they understand what the proxy layer is doing. (You need to assume the proxy layer is self contained and works as documented)
Clever because it hides complexities that the programmer probably doesn’t want/need to deal with. The idea is hide the boring nuts and bolts (like DBMS persistence) and let the programmer focus on the functional bits.
It’s just another abstraction. The point at which layered abstractions become more complicated than a user can understand is dependent on the individual.
it’s far from an anti-pattern. Example: the OpenStep framework, which heavily uses delegation. Example in the example — tableview (http://developer.apple.com/documentation/Cocoa/Reference/Applicatio…). In some other programming framework, you need to create a new class that herits from the tableview widget to define what appears (the datas) in the tableview. It’s frankly a bad design, as you unnecessary create a child class while you don’t modify the behavior of the class.
In OpenStep, you just create a “data source” object that answers two messages : 1) how many rows in the table 2) what’s the item row i column j
Advantage: you still use NSTableView — no child class — and the data source object is as simple as you want.
For more general delegations, it’s also quite useful — basically, it lets you NOT subclass things when it’s not necessary, and you just want to tweak a few things. For example, NSTableView can send a message to the delegate before displaying a cell — letting the delegate modifying the cell behavior if needed.
To sum up, subclassing can be problematic (particularly for complex classes), and delegation is quite often much clearer, as you have a very simple object.
Sometimes subclassing is needed, but nothing prevents you to use it 😉
But it’s not needed 95% of the time, so it’s better to have simpler objects thanks to delegation.
You’re talking about multiple inheritance, but it’s not the silver bullet (to the contrary imho..). If only because of the diamond problem. In any way, delegation is far from an anti pattern — it’s an extremely useful one.
ideas like traits are also probably worth looking:
IMHO, we have too much java on OSNews :/
Why?
It balances out the .NET news. Dynamic Delegation is a little known feature of Java that gives it the power to do some very flexible things (sort of like Smalltalk’s “cannotunderstand” message handler). Without it, MacOSX’s Java API to their Objective-C API might not be possible without a lot of ugly kludges.
Personnally, I wouldn’t mind hearing more OS-related news about Python (Linux’s current Visual Basic) and Objective-C (MacOSX’s API and the GNUstep API).
I agree, not that much java news imho, and dynamic delegates in java are widely unknown. Anyway, I guess you meant doesNotUnderstand: in smalltalk
I read through the article and am still having a difficult time understanding what exactly this feature is. Can someone summarize it for me – thanks
It’s simply a Java feature that improves upon the Proxy design pattern. If you’re unfamiliar w/ OOP than it would mean very little to you.
Linux’s current Visual Basic? This is what i can’t understand…
@Anonymous: Where you’d use this feature is if you wanted to write some code to wrap method calls on a class or something implementing an interface. E.g. you want to intercept method call X to this class and do something else before you call the method.
So to everywhere that uses this class, you pass the proxy that you’ve created instead of the real instance.
http://en.wikipedia.org/wiki/Delegation_pattern
The “delegation pattern” really just means to wrap a method from another class, using the same name and no additional code.
in C++:
class A::do_work(){ /* do actual work */ }
class B::do_work(){ m_a.do_work(); } // delegate work to A
Why would you use this?
1. If you’ve got a bodged design where your high level classes cannot be used to satisfy their functional requirements. Solution: make a delegation class, (or spaghetti class) that wraps a bunch of functionality from other classes.
2. If you’ve got a bunch of low level simple classes that are functionally atomic (or have a bunch of static functions) and you want to pull them all together into one super class.
3. If your object oriented language does not have multiple inheretance, simply wrap mutiple classes using “delegation”.
I consider this an anti-pattern because it works against functionally orthogonal classes.
Delegation is also used to enhance basic classes at runtime with extra features. Like dynamic persistence (ala Hibernate)
This is both dumb and clever.
It’s dumb because it becomes impossible for the programmer to know what’s happening unless they understand what the proxy layer is doing. (You need to assume the proxy layer is self contained and works as documented)
Clever because it hides complexities that the programmer probably doesn’t want/need to deal with. The idea is hide the boring nuts and bolts (like DBMS persistence) and let the programmer focus on the functional bits.
It’s just another abstraction. The point at which layered abstractions become more complicated than a user can understand is dependent on the individual.
it’s far from an anti-pattern. Example: the OpenStep framework, which heavily uses delegation. Example in the example — tableview (http://developer.apple.com/documentation/Cocoa/Reference/Applicatio…). In some other programming framework, you need to create a new class that herits from the tableview widget to define what appears (the datas) in the tableview. It’s frankly a bad design, as you unnecessary create a child class while you don’t modify the behavior of the class.
In OpenStep, you just create a “data source” object that answers two messages : 1) how many rows in the table 2) what’s the item row i column j
Advantage: you still use NSTableView — no child class — and the data source object is as simple as you want.
For more general delegations, it’s also quite useful — basically, it lets you NOT subclass things when it’s not necessary, and you just want to tweak a few things. For example, NSTableView can send a message to the delegate before displaying a cell — letting the delegate modifying the cell behavior if needed.
To sum up, subclassing can be problematic (particularly for complex classes), and delegation is quite often much clearer, as you have a very simple object.
Sometimes subclassing is needed, but nothing prevents you to use it 😉
But it’s not needed 95% of the time, so it’s better to have simpler objects thanks to delegation.
You’re talking about multiple inheritance, but it’s not the silver bullet (to the contrary imho..). If only because of the diamond problem. In any way, delegation is far from an anti pattern — it’s an extremely useful one.
ideas like traits are also probably worth looking:
http://www.iam.unibe.ch/~scg/Research/Traits/