Delegation pattern
Encyclopedia : D : DE : DEL : Delegation pattern
In software engineering, the delegation pattern is a technique where an object outwardly expresses certain behaviour but in reality delegates responsibility for implementing that behavior to an associated object in an Inversion of Responsibility. The delegation pattern is the fundamental abstraction that underpins composition (also referred to as aggregation), mixins and aspects.
Applicability Example using PHP
Say you write a parent class to model cats, then extend the class to specific cats as follows
class Cat function Snuggle() function Eat() function Sleep() }This code is fine and dandy, however, lets say you watch a documentary about lions and realize you didn't model lions, at the same time you also think you would like to add in cheetahs and tigers, so you set about implementing the followingclass Tabby extends Cat }
class BlackCat extends Cat }
Lion class.
class Lion extends Cat function Roar() function Snuggle() function Sleep() function Eat() function Draw() }So now you have a lion class, however you also have a new
roar() method. Now you have to edit all your code to test if this is a lion, and if so call roar instead of meow. You realize that tigers also roar, sleep under trees and don't snuggle well, but they don't eat gazelles. Perhaps you could make a big cat class and extend it to tigers and lions and override the base classes. But then you realize a cheetah is a big cat that sleeps under trees and eat gazelles, but they don't roar. This is getting messier by the minute. Next you see another documentary with civet cats and they eat birds and sleep in the trees and make a different sound. Oh no! Your code is now a disaster filled with classes extended left and right with all kinds of methods being overridden. On top of all this you also realize that if you end up with hundreds of cats whom sleep under trees that override the sleep method and want to change what that method does, you will need to edit every single place that is overridden.Lets try this a little differently and define an abstract cat interface as follows:
abstract ICat function Snuggle() function Eat() function Sleep() function Draw() function setSoundBehavior( $SoundBehavior ) function setSnuggleBehavior( $SnuggleBehavior ) function setEatBehavior( $EatBehavior ) function setSleepBehavior( $SleepBehavior ) }Now create an interface for each behavior, then an implementation using SoundBehavior as an example (each behavior would need its own interface/implementation):
interface SoundBehavior } class Roar implements SoundBehavior } class Meow implements SoundBehavior }Assuming you created all your behaviours, lets define a lion and a cat
class HouseCat implements ICat function Draw() }This final abstract interfaceclass Tabby Extends HouseCat }
class Tiger implements ICat function Draw() }
ICat, delegates responsibility of cat behavior instead of using methods in a base class and overriding them where needed. If we didn't use delegation above we would need to override base method classes repeatedly in subclasses. Additional classes can be added which share some, but not all, of the cat functionality using a different set of delegates.Examples
Simple Java example
In this Java programming language example, the class C has method stubs that forward the methods f() and g() to class A. Class C pretends that it has attributes of class A.
class A void g() }class C void g()
// normal attributes X x = new X(); void y() }
public class Main }
Complex Java example
By using interfaces, delegation can be made more flexible and typesafe. In this example, class C can delegate to either class A or class B. Class C has methods to switch between classes A and B. Including the implements clauses improves type safety, because each class must implement the methods in the interface. The main tradeoff is more code.
interface Iclass A implements I public void g() }
class B implements I public void g() }
class C implements I public void g()
// normal attributes void toA() void toB() }
public class Main }
Complex C++ example
This example is a C++ version of the complex Java example above. Since C++ does not have an interface construct, a pure virtual class plays the same role. The advantages and disadvantages are largely the same as in the Java example.
#includejavausing namespace std; class I ;
class A : public I void g() };
class B : public I void g() };
class C : public I virtual ~C()
private: // delegation I* i;
public: void f() void g()
// normal attributes void toA() void toB() };
int main()
Criticisms
This pattern typically sacrifices speed optimization in favor of enhanced clarity of abstraction.
See also
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
