World Library  
Flag as Inappropriate
Email this Article

Delegation pattern

Article Id: WHEBN0000148655
Reproduction Date:

Title: Delegation pattern  
Author: World Heritage Encyclopedia
Language: English
Subject: Fundamental pattern, Helper class, Articles with example Java code, Indirection, Delegation (programming)
Publisher: World Heritage Encyclopedia

Delegation pattern

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. There is an Inversion of Responsibility in which a helper object, known as a delegate, is given the responsibility to execute a task for the delegator. 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.


  • Examples 1
    • Java examples 1.1
      • Simple 1.1.1
      • Complex 1.1.2
  • See also 2
  • External links 3


Java examples


In this Java example, the
Class (computer science) has a
method. This print method, rather than performing the print itself, delegates to class
. To the outside world it appears that the
class is doing the print, but the
class is the one actually doing the work.

Delegation is simply passing a duty off to someone/something else. Here is a simple example:

 class RealPrinter {    // the "delegate"
        void print() { 

 class Printer {        // the "delegator"
        RealPrinter p = new RealPrinter();      // create the delegate 
        void print() { 
                p.print();      // delegation
 public class Main {
        // to the outside world it looks like Printer actually prints.
        public static void main(String[] arguments) {
                Printer printer = new Printer();


The more complex case is a Decorator Pattern that by using interfaces, delegation can be made more flexible and typesafe. "Flexibility" here means that
need not refer to
in any way, as the switching of delegation is abstracted from
. In this example, class
can delegate to any class that implements
. Class
has a method to switch to another delegator. Including the
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()"); }
// changing the implementing object in run-time (normally done in compile time)
class C implements I {
        I i = null;
        // delegation
        public C(I i){ setI(i); }
        public void f() { i.f(); }
        public void g() { i.g(); }
        // normal attributes
        public void setI(I i) { this.i = i; }
public class Main {
        public static void main(String[] arguments) {
                C c = new C(new A());
                c.f();  // output: A: doing f()
                c.g();  // output: A: doing g()
                c.setI(new B());
                c.f();  // output: B: doing f()
                c.g();  // output: B: doing g()

See also

External links

This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from World eBook Library are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.