Delegation means handing over the responsibility for a particular task to another class or method.
It is a technique where an object expresses certain behavior to the outside but in reality delegates responsibility for implementing that behavior to an associated object.
Java Delegation Example
In this example, the delegates are CanonPrinter, EpsonPrinter, or HpPrinter and they all implement the Printer interface. The PrinterController is a delegator class that 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.
Step 1: First create a 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: Specialized Implementation of Printer for an 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: Specialized Implementation of Printer for an 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 a 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 is more than one implementor 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 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);
}
}
Related Java OOPS Examples
- Java Abstraction Example
- Java Inheritance Example
- Java Encapsulation Example
- Java Simple Inheritance Example
- Java Composition Example
- Java Aggregation Example
- Java Delegation Example
- Java Method Overloading Example
- Java Method Overriding Example
- Java Single Inheritance Example
- Java Multilevel Inheritance Example
- Java Hierarchical Inheritance Example
- Java Abstract Class Example
- Java Class Example
Comments
Post a Comment