Google
 
   
Login
Username:

Password:


Lost Password?

Register now!
Search
Main Menu
top books
Polls
What do you think about php-deluxe.net?
Excellent!
Cool
Hmm..not bad
What the hell is this?
encyclopedia
recommendation
Freenet DSL
Who's Online
6 user(s) are online (5 user(s) are browsing encyclopedia)

Members: 0
Guests: 6

more...
browser tip
recommendation!
Sponsored
partner

Delegation pattern

In software engineering, the delegation pattern is a technique where an object (computer science) outwardly expresses certain behaviour but in reality delegates responsibility for implementing that behavior to an associated object in an Inversion of Responsibility . This simulates mixins, delegation, and some kinds of aspects in traditional object-oriented languages like C plus plus and Java programming language. Aggregation must be used with it if inheritance (computer science) is not applicable.

=Examples=

==Simple Java example==

In this example, the class (computer science) C has method stubs that forward the method (computer science)s f() and g() to class A. Class C pretends that it has attributes of class A.

class A { void f() { System.out.println( A: doing f() ); } void g() { System.out.println( A: doing g() ); } }

class C { // delegation A a = new A();

void f() { a.f(); } void g() { a.g(); }

// normal attributes X x = new X(); void y() { /* do stuff */ } }

void main() { C c = new C();

c.f(); c.g(); }

==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 I { void f(); void g(); }

class A implements I { public void f() { System.out.println( A: doing f() ); } public void g() { System.out.println( A: doing g() ); } }

class B implements I { public void f() { System.out.println( B: doing f() ); } public void g() { System.out.println( B: doing g() ); } }

class C implements I { // delegation I i = new A();

public void f() { i.f(); } public void g() { i.g(); }

// normal attributes void toA() { i = new A(); } void toB() { i = new B(); } }

void main() { C c = new C();

c.f(); c.g(); }

==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.

#include using namespace std;

class I { public: virtual void f() = 0; virtual void g() = 0; };

class A : public I { public: void f() { cout