Delegation Pattern

Intent

It is a technique where an object expresses certain behavior to the outside but in reality delegates responsibility for implementing that behaviour to an associated object.

Also known as

Proxy Pattern

Explanation

Delegation is a way of reusing and extending the behavior of a class. It works writing a new class that incorporates the functionality of the original class by using an instance of the original class and calling its methods.
The advantage of delegation is that it is easy to compose behavior at runtime.

Implementation

Let's take an example of Printers Implementation.
 In this example the delegates are CanonPrinter, EpsonPrinter or HpPrinter they all implement Printer. The PrinterController is a delegator class which also implements Printer.
PrinterController is not responsible for the actual desired action but is actually delegated to a helper class either CanonPrinter, EpsonPrinter or HpPrinter. The consumer does not have or require knowledge of the actual class carrying out the action, only the container on which they are calling.
You can observe here the implementation is loosely coupled.

Class Diagram


Source code

Step 1: First create Printer interface that both the Controller and the Delegate classes will implement.
public interface Printer {
  void print(final String message);
}
Step 2: Specialised Implementation of Printer for a Canon Printer, in this case the message to be printed is appended to "Canon Printer : ".
public class CanonPrinter implements Printer {

  private static final Logger LOGGER = LoggerFactory.getLogger(CanonPrinter.class);
  @Override
  public void print(String message) {
    LOGGER.info("Canon Printer : {}", message);
  }

}
Step 3: Specialised Implementation of Printer for a Epson Printer, in this case the message to be printed is appended to "Epson Printer : ".
public class EpsonPrinter implements Printer {

  private static final Logger LOGGER = LoggerFactory.getLogger(EpsonPrinter.class);
  @Override
  public void print(String message) {
    LOGGER.info("Epson Printer : {}", message);
  }

}
Step 4: Specialised Implementation of Printer for a HP Printer, in this case the message to be printed is appended to "HP Printer : ".
public class HpPrinter implements Printer {

  private static final Logger LOGGER = LoggerFactory.getLogger(HpPrinter.class);
  @Override
  public void print(String message) {
    LOGGER.info("HP Printer : {}", message);
  }

}
Step 5: it's time to implement Delegator class.
 Delegator Class to delegate the implementation of the Printer.
 This ensures two things:
  • when the actual implementation of the Printer class changes the delegation will still be operational
  • the actual benefit is observed when there are more than one implementors and they share a delegation control.
public class PrinterController implements Printer {

  private final Printer printer;

  public PrinterController(Printer printer) {
    this.printer = printer;
  }
  @Override
  public void print(String message) {
    printer.print(message);
  }
}
Step 6: Let's test the Delegation pattern using the main method.
public class App {

  public static final String MESSAGE_TO_PRINT = "hello world";

  /**
   * Program entry point
   *
   * @param args command line args
   */
  public static void main(String[] args) {
    PrinterController hpPrinterController = new PrinterController(new HpPrinter());
    PrinterController canonPrinterController = new PrinterController(new CanonPrinter());
    PrinterController epsonPrinterController = new PrinterController(new EpsonPrinter());

    hpPrinterController.print(MESSAGE_TO_PRINT);
    canonPrinterController.print(MESSAGE_TO_PRINT);
    epsonPrinterController.print(MESSAGE_TO_PRINT);
  }

}

Applicability

Use the Delegate pattern in order to achieve the following
  • Reduce the coupling of methods to their class
  • Components that behave identically, but realize that this situation can change in the future.

References

Comments