The delegate pattern is a fundamental technique in software design patterns. In the delegate pattern, two objects participate in handling the same request, and the object receiving the request delegates the request to another object for processing. The delegate pattern is a fundamental technique; many other patterns, such as the state pattern, strategy pattern, and visitor pattern, essentially employ the delegate pattern in more specific situations. The delegate pattern allows us to use aggregation instead of inheritance, and it also enables us to simulate mixins.

Simple Java Example

In this example, the class simulating a printer, Printer, holds an instance of the dot-matrix printer RealPrinter, and the print() method of Printer delegates the processing to the print() method of RealPrinter.

 class RealPrinter { // the "delegate"
     void print() { 
       System.out.print("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[] args) {
         Printer printer = new Printer();
         printer.print();
     }
 }

Complex Java Example

By using interfaces, delegation can achieve type safety and be more flexible. In this example, class C can delegate to class A or class B, and class C has methods that allow it to choose between class A or class B. Because class A or class B must implement the methods defined by interface I, the delegation here is type-safe. This example shows that the disadvantage of delegation is that it requires 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()"); }
 }
 
 class C implements I {
     // delegation
     I i = new A();
 
     public void f() { i.f(); }
     public void g() { i.g(); }
 
     // normal attributes
     public void toA() { i = new A(); }
     public void toB() { i = new B(); }
 }
 
 
 public class Main {
     public static void main(String[] args) {
         C c = new C();
         c.f();     // output: A: doing f()
         c.g();     // output: A: doing g()
         c.toB();
         c.f();     // output: B: doing f()
         c.g();     // output: B: doing g()
     }
 }

Source: https://zh.wikipedia.org/wiki/%E5%A7%94%E6%89%98%E6%A8%A1%E5%BC%8F