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. This simulates mixins, delegation, and some kinds of aspects in traditional object-oriented languages like C++ and Java. Aggregation must be used with it if inheritance is not applicable.

Contents

Examples

Simple Java example

In this 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 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 {
  void f() { System.out.println("A: doing f()"); }
  void g() { System.out.println("A: doing g()"); }
}

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

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

  void f() { i.f(); }
  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 <iostream>

using namespace std;

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

class A : public I {
public:
  void f() { cout << "A: doing f()" << endl; }
  void g() { cout << "A: doing g()" << endl; }
};

class B : public I {
public:
  void f() { cout << "B: doing f()" << endl; }
  void g() { cout << "B: doing g()" << endl; }
};

class C : public I {
public:
  // construction/destruction
  C() : i( new A() ) { }
  virtual ~C() { delete i; }
  
private:
  // delegation
  I* i;

public:
  void f() { i->f(); }
  void g() { i->g(); }

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

int main() {
  C* c = new C();

  c->f();
  c->g();
}

Criticisms

Because this is a pattern, developers can make many kinds of mistakes. The developer could forget to delegate a method in the simple version. The developer could mistype the name of one attribute.

This pattern typically sacrifices speed optimization in favor of enhanced clarity of abstraction.

See also

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools