Will Fris's WordPress Weblog











{20101110}   Delegation Pattern

Delegation Pattern

Intro

The delegation came to my interest because i wanted to create an more abstract way of composing form-elements used for database-input.
At first i thought about scaffolding, but this is not really the type of abstraction i’m looking for.
Scaffolding at this moment seems to be to me an abstraction of solving-the-problem, instead of an abstraction of the problem itself.

At the moment this is more of a note then anything else.
The examples i give about the delegation pattern on this page are in my opinion quite useless…
I’ve already put them down here though, for fun’s sake( not blaming pete here).

Some Wikipedia-quotes

I’ve quickly collected what seemed to be the most useful sentences in a few wikipedia-articles.
It is my plan to sometime ellaborate on these quotes.

Design patterns were originally grouped into the categories: creational patterns, structural patterns, and behavioral patterns, and described using the concepts of delegation, aggregation, and consultation.

In its original usage, delegation refers to one object relying upon another to provide a specified set of functionalities. In research, this is often referred to as consultation or as aggregation in modeling.

In software engineering, the delegation pattern is a design pattern in object-oriented programming where an object, instead of performing one of its stated tasks, delegates that task to an associated helper object. It passes the buck, so to speak (technically, an Inversion of Responsibility). The helper object is called the delegate. The delegation pattern is one of the fundamental abstraction patterns that underlie other software patterns such as composition (also referred to as aggregation), mixins and aspects.

The short definition is that delegation defines method dispatching the way it is defined for virtual methods in inheritance: It is always the most specific method which is chosen during method-lookup. Hence it is the original receiver entity which is the start of method lookup even though it has passed on control to some other object (through a delegation link, not an object reference). Delegation has the advantage that it can take place at run-time and affect only a subset of entities of some type and can even be removed at run-time. Inheritance on the other hand typically targets the type rather than the instances and is restricted to compile time. On the other hand, inheritance can be statically type-checked while delegation generally cannot without generics (G. Kniesel has shown that a restricted version of delegation can be statically typesafe). Delegation can be termed “run-time inheritance for specific objects”.

Examples

Shamelessly taken from wikipedia’s “Simple Java example” :


class RealPrinter { // the "delegate"
public function print () {
print( "something" );
}
}
class Printer { // the "delegator"
private $p;//@var $p RealPrinter
public function __construct () {
$this->p = new RealPrinter(); // create the delegate
}
public function print() {
$this->p->print();// delegation
}
}
class Main {
// to the outside world it looks like Printer actually prints.
public function __construct () {
$printer = new Printer();
$printer->print();
}
}
new Main();

Shamelessly taken from wikipedia’s “Complex Java example” :


interface I {
public function f();
public function g();
}
class A implements I {
public function f() {
print( "A: doing f()" );
}
public function g() {
print( "A: doing g()" );
}
}
class B implements I {
public function f() {
print( "B: doing f()" );
}
public function g() {
print( "B: doing g()" );
}
}
class C implements I {
// delegation
private $i;// implements interface I->
public function __construct () {
$this->i = new A();
}
public function f() {
$this->i->f();
}
public void g() {
$this->i->g();
}

// normal attributes
public function toA () {
$this->i = new A();
}
public function toB () {
$this->i = new B();
}
}
class Main {
public function __construct () {
$c = new C();
$c->f();// output: A: doing f()
$c->g();// output: A: doing g()
$c->toB();
$c->f();// output: B: doing f()
$c->g();// output: B: doing g()
}
}

Also see

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

et cetera
%d bloggers like this: