Delegation

Delegation is midway between inheritance and composition, because you place a member object in the class you’re building (like composition), but at the same time you expose all the methods from the member object in your new class (like inheritance). For example,

// reuse/SpaceShipControls.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

public class SpaceShipControls {
  void up(int velocity) {}

  void down(int velocity) {}

  void left(int velocity) {}

  void right(int velocity) {}

  void forward(int velocity) {}

  void back(int velocity) {}

  void turboBoost() {}
}

 

// reuse/SpaceShipDelegation.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

public class SpaceShipDelegation {
  private String name;
  private SpaceShipControls controls = new SpaceShipControls();

  public SpaceShipDelegation(String name) {
    this.name = name;
  }
  // Delegated methods:
  public void back(int velocity) {
    controls.back(velocity);
  }

  public void down(int velocity) {
    controls.down(velocity);
  }

  public void forward(int velocity) {
    controls.forward(velocity);
  }

  public void left(int velocity) {
    controls.left(velocity);
  }

  public void right(int velocity) {
    controls.right(velocity);
  }

  public void turboBoost() {
    controls.turboBoost();
  }

  public void up(int velocity) {
    controls.up(velocity);
  }

  public static void main(String[] args) {
    SpaceShipDelegation protector = new SpaceShipDelegation("NSEA Protector");
    protector.forward(100);
  }
}

Although the Java language doesn’t support delegation, development tools often do. The above example, for instance, was automatically generated using the JetBrains Idea IDE.

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/reuse/SpaceShipControls.java

3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/reuse/SpaceShipDelegation.java

### Java 中的 Delegation 模式及用法 #### 委托模式的概念 委托模式是一种面向对象编程的设计模式,其中一个对象将其部分职责交给另一个对象来处理。这种模式允许将行为封装到独立的对象中,并通过组合而非继承的方式构建复杂的行为。 在Java中,可以通过创建一个持有其他对象实例变量的类来实现委托模式。当调用该类的方法时,它会转发请求给内部持有的对象去完成具体的业务逻辑[^1]。 #### 实现方式 下面是一个简单的例子展示了如何利用`Manager`类作为委托者,而不同的`Employee`子类则扮演被委托的角色: ```java // 定义任务接口 public interface Task { void doingtask(String taskName); } // 不同的任务由不同员工负责 class EmployeeA implements Task{ @Override public void doingtask(String taskName){ System.out.println("Employee A is handling " + taskName); } } class EmployeeB implements Task{ @Override public void doingtask(String taskName){ System.out.println("Employee B is handling " + taskName); } } // 经理类作为委托者 public class Manager implements Task { private final Map<String, Task> tasks; public Manager(){ this.tasks = new HashMap<>(); tasks.put("任务A", new EmployeeA()); tasks.put("任务B", new EmployeeB()); } @Override public void doingtask(String s){ if (tasks.containsKey(s)){ tasks.get(s).doingtask(s); } else { System.out.println("No employee assigned to handle " + s); } } } ``` 在这个例子中,`Manager`类实现了`Task`接口并通过构造函数初始化了一个哈希表用来存储不同类型的任务与其对应的执行者之间的映射关系。每当有新的任务到来时,`Manager`就会查找相应的`Employee`并将工作委派给他们去做。 #### 应用场景 除了上述提到的例子外,委托模式还可以应用于许多其他的场合,比如UI组件事件处理器、日志记录器等。它可以有效地降低模块间的耦合度,提高系统的可维护性和灵活性[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值