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)
Collection:
Publisher: World Heritage Encyclopedia
Publication
Date:
 

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.

Contents

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

Examples

Java examples

Simple

In this Java example, the
Printer
Class (computer science) has a
print
method. This print method, rather than performing the print itself, delegates to class
RealPrinter
. To the outside world it appears that the
Printer
class is doing the print, but the
RealPrinter
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() { 
                System.out.println("something"); 
        }
 }

 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();
                printer.print();
        }
 }

Complex

The more complex case is a Decorator Pattern that by using interfaces, delegation can be made more flexible and typesafe. "Flexibility" here means that
C
need not refer to
A
or
B
in any way, as the switching of delegation is abstracted from
C
. In this example, class
C
can delegate to any class that implements
I
. Class
C
has a method to switch to another delegator. 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()"); }
}
 
// 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 USA.gov, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for USA.gov 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.