* update yaml frontmatter format * update abstract document * update abstract factory * use the new pattern template * acyclic visitor seo * adapter seo * ambassador seo * acl seo * aaa seo * async method invocation seo * balking seo * bridge seo * builder seo * business delegate and bytecode seo * caching seo * callback seo * chain seo * update headings * circuit breaker seo * client session + collecting parameter seo * collection pipeline seo * combinator SEO * command seo * cqrs seo * commander seo * component seo * composite seo * composite entity seo * composite view seo * context object seo * converter seo * crtp seo * currying seo * dao seo * data bus seo * data locality seo * data mapper seo * dto seo * decorator seo * delegation seo * di seo * dirty flag seo * domain model seo * double buffer seo * double checked locking seo * double dispatch seo * dynamic proxy seo * event aggregator seo * event-based asynchronous seo * eda seo * event queue seo * event sourcing seo * execute around seo * extension objects seo * facade seo * factory seo * factory kit seo * factory method seo * fanout/fanin seo * feature toggle seo * filterer seo * fluent interface seo * flux seo * flyweight seo * front controller seo * function composition seo * game loop seo * gateway seo * guarded suspension seo * half-sync/half-async seo * health check seo * hexagonal seo * identity map seo * intercepting filter seo * interpreter seo * iterator seo * layers seo * lazy loading seo * leader election seo * leader/followers seo * lockable object seo * rename and add seo for marker interface * master-worker seo * mediator seo * memento seo * metadata mapping seo * microservice aggregator seo * api gw seo * microservices log aggregration seo * mvc seo * mvi seo * mvp seo * mvvm seo * monad seo * monitor seo * monostate seo * multiton seo * mute idiom seo * naked objects & notification seo * null object seo * object mother seo * object pool seo * observer seo * optimistic locking seo * page controller seo * page object seo * parameter object seo * partial response seo * pipeline seo * poison pill seo * presentation model seo * private class data seo * producer-consumer seo * promise seo * property seo * prototype seo * proxy seo * queue-based load leveling seo * reactor seo * registry seo * repository seo * RAII seo * retry seo * role object seo * saga seo * separated interface seo * serialized entity seo * serialized lob seo * servant seo * server session seo * service layer seo * service locator seo * service to worker seo * sharding seo * single table inheritance seo * singleton seo * spatial partition seo * special case seo * specification seo * state seo * step builder seo * strangler seo * strategy seo * subclass sandbox seo * table module seo * template method seo * throttling seo * tolerant reader seo * trampoline seo * transaction script seo * twin seo * type object seo * unit of work seo * update method seo * value object seo * version number seo * virtual proxy seo * visitor seo * seo enhancements * seo improvements * SEO enhancements * SEO improvements * SEO additions * SEO improvements * more SEO improvements * rename hexagonal + SEO improvements * SEO improvements * more SEO stuff * SEO improvements * SEO optimizations * SEO enhancements * enchance SEO * improve SEO * SEO improvements * update headers
title, shortTitle, description, category, language, tag
| title | shortTitle | description | category | language | tag | |||
|---|---|---|---|---|---|---|---|---|
| Delegation Pattern in Java: Mastering Efficient Task Assignment | Delegation | Explore the Delegation Design Pattern in Java with real-world examples, class diagrams, and its benefits. Learn how to enhance your code flexibility and reuse. | Behavioral | en |
|
Also known as
- Helper
- Surrogate
Intent of Delegation Design Pattern
To allow an object to delegate responsibility for a task to another helper object.
Detailed Explanation of Delegation Pattern with Real-World Examples
Real-world example
In a restaurant, the head chef delegates tasks to sous-chefs: one manages grilling, another handles salads, and a third is in charge of desserts. Each sous-chef specializes in their area, allowing the head chef to focus on overall kitchen management. This mirrors the Delegation design pattern, where a main object delegates specific tasks to helper objects, each expert in their domain.
In plain words
Delegation is a design pattern where an object passes on a task to a helper object.
Wikipedia says
In object-oriented programming, delegation refers to evaluating a member (property or method) of one object (the receiver) in the context of another original object (the sender). Delegation can be done explicitly, by passing the sending object to the receiving object, which can be done in any object-oriented language; or implicitly, by the member lookup rules of the language, which requires language support for the feature.
Programmatic Example of Delegation Pattern in Java
Let's consider a printing example.
We have an interface Printer and three implementations CanonPrinter, EpsonPrinter and HpPrinter.
public interface Printer {
void print(final String message);
}
@Slf4j
public class CanonPrinter implements Printer {
@Override
public void print(String message) {
LOGGER.info("Canon Printer : {}", message);
}
}
@Slf4j
public class EpsonPrinter implements Printer {
@Override
public void print(String message) {
LOGGER.info("Epson Printer : {}", message);
}
}
@Slf4j
public class HpPrinter implements Printer {
@Override
public void print(String message) {
LOGGER.info("HP Printer : {}", message);
}
}
The PrinterController can be used as a Printer by delegating any work handled by this interface to an object implementing it.
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);
}
}
In the client code, printer controllers can print messages differently depending on the object they're delegating that work to.
public class App {
private static final String MESSAGE_TO_PRINT = "hello world";
public static void main(String[] args) {
var hpPrinterController = new PrinterController(new HpPrinter());
var canonPrinterController = new PrinterController(new CanonPrinter());
var epsonPrinterController = new PrinterController(new EpsonPrinter());
hpPrinterController.print(MESSAGE_TO_PRINT);
canonPrinterController.print(MESSAGE_TO_PRINT);
epsonPrinterController.print(MESSAGE_TO_PRINT);
}
}
Program output:
HP Printer:hello world
Canon Printer:hello world
Epson Printer:hello world
Detailed Explanation of Delegation Pattern with Real-World Examples
When to Use the Delegation Pattern in Java
- When you want to pass responsibility from one class to another without inheritance.
- To achieve composition-based reuse instead of inheritance-based.
- When you need to use several interchangeable helper classes at runtime.
Real-World Applications of Delegation Pattern in Java
- Java's java.awt.event package, where listeners are often used to handle events.
- Wrapper classes in Java's Collections Framework (java.util.Collections), which delegate to other collection objects.
- In Spring Framework, delegation is used extensively in the IoC container where beans delegate tasks to other beans.
Benefits and Trade-offs of Delegation Pattern
Benefits:
- Reduces subclassing: Objects can delegate operations to different objects and change them at runtime, reducing the need for subclassing.
- Encourages reuse: Delegation promotes the reuse of the helper object's code.
- Increases flexibility: By delegating tasks to helper objects, you can change the behavior of your classes at runtime.
Trade-offs:
- Runtime Overhead: Delegation can introduce additional layers of indirection, which may result in slight performance costs.
- Complexity: The design can become more complicated since it involves additional classes and interfaces to manage delegation.
Related Java Design Patterns
- Composite: Delegation can be used within a composite pattern to delegate component-specific behavior to child components.
- Strategy: Delegation is often used in the strategy pattern where a context object delegates tasks to a strategy object.
- https://java-design-patterns.com/patterns/proxy/: The proxy pattern is a form of delegation where a proxy object controls access to another object, which it delegates work to.
