复用类

1代理:它是Java第三种关系,这是继承与组合的中庸之道,因为我们将一个成员对象置于所要构造的类中(就像组合),但与此同时我们在新类中暴露了该成员对象的所有方法(就像继承),而代理可以拥有更多的控制力,致我们可以选择只提供在成员对象中的方法的某个子集。代理解析图:      


代码例子:

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() {}
}
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);
  }
}
2确保正确清理,在清理方法中(dispose())中,必须注意对基类清理方法和成员对象清理方法的调用顺序,以放某个子对象依赖于另一个子对象情形的发生,执行类的所有特定的清理动作,其顺序同生成顺序相反(通常这就要求基类元素仍旧存活)。

代码例子:

class Shape {
	  Shape(int i) { out.println("Shape constructor"); }
	  void dispose() { out.println("Shape dispose"); }
	}

	class Circle extends Shape {
	  Circle(int i) {
	    super(i);
	    out.println("Drawing Circle");
	  }
	  void dispose() {
	    out.println("Erasing Circle");
	    super.dispose();
	  }
	}

	class Triangle extends Shape {
	  Triangle(int i) {
	    super(i);
	    out.println("Drawing Triangle");
	  }
	  void dispose() {
	    out.println("Erasing Triangle");
	    super.dispose();
	  }
	}

	class Line extends Shape {
	  private int start, end;
	  Line(int start, int end) {
	    super(start);
	    this.start = start;
	    this.end = end;
	    out.println("Drawing Line: " + start + ", " + end);
	  }
	  void dispose() {
	    out.println("Erasing Line: " + start + ", " + end);
	    super.dispose();
	  }
	}

	public class CADSystem extends Shape {
	  private Circle c;
	  private Triangle t;
	  private Line[] lines = new Line[3];
	  public CADSystem(int i) {
	    super(i + 1);
	    for(int j = 0; j < lines.length; j++)
	      lines[j] = new Line(j, j*j);
	    c = new Circle(1);
	    t = new Triangle(1);
	    out.println("Combined constructor");
	  }
	  public void dispose() {
	    out.println("CADSystem.dispose()");
	    // The order of cleanup is the reverse
	    // of the order of initialization:
	    t.dispose();
	    c.dispose();
	    for(int i = lines.length - 1; i >= 0; i--)
	      lines[i].dispose();
	    super.dispose();
	  }
	  public static void main(String[] args) {
	    CADSystem x = new CADSystem(47);
	    try {
	      // Code and exception handling...
	    } finally {
	      x.dispose();
	    }
	  }
	}
运行结果:

Shape constructor
Shape constructor
Drawing Line: 0, 0
Shape constructor
Drawing Line: 1, 1
Shape constructor
Drawing Line: 2, 4
Shape constructor
Drawing Circle
Shape constructor
Drawing Triangle
Combined constructor
CADSystem.dispose()
Erasing Triangle
Shape dispose
Erasing Circle
Shape dispose
Erasing Line: 2, 4
Shape dispose
Erasing Line: 1, 1
Shape dispose
Erasing Line: 0, 0
Shape dispose
Shape dispose
3组合技术通常用于想在新类中使用现有类的功能而非它的接口这种情形,即在新类中嵌入某个对象,让其实现所需要的功能,但新类的用户看到的只是为新类所定义的接口,而非所嵌入对象的接口。有事,让类的用户直接访问新类中的组合成分是极具意义的,也就让成员对象声明为public(成员对象自身都隐藏了具体实现,那么这种做法是安全的),这有助于客户端程序员了解怎么样去使用类,而且降低了类开发者所面临的代码复杂度。(不过一般情况下成员变量为private)。

代码例子:

class Engine {
  public void start() {}
  public void rev() {}
  public void stop() {}
}

class Wheel {
  public void inflate(int psi) {}
}

class Window {
  public void rollup() {}
  public void rolldown() {}
}

class Door {
  public Window window = new Window();
  public void open() {}
  public void close() {}
}

public class Car {
  public Engine engine = new Engine();
  public Wheel[] wheel = new Wheel[4];
  public Door
    left = new Door(),
    right = new Door(); // 2-door
  public Car() {
    for(int i = 0; i < 4; i++)
      wheel[i] = new Wheel();
  }
  public static void main(String[] args) {
    Car car = new Car();
    car.left.window.rollup();
    car.wheel[0].inflate(72);
  }
}

这篇博客参考资料:

thinking in Java





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值