范例(Examples)
本例从两个classes开始,代表[人]的Person和代表[部门]的Department:
class Person {
Department _department;
public Department getDepartment() {
return _department;
}
public void setDepartment(Department arg) {
_department = arg;
}
}
class Department {
private String _chargeCode;
private Person _manager;
public Department(Person manager) {
_manager = manager;
}
public Person getManager() {
return _manager;
}
...
如果客户希望知道某人的经理是谁,他必须先取得Department对象:
manager = john.getDepartment().getManager();
这样的编码就是对客户揭露了Department工作原理,于是客户知道:Department用以追踪[经理]这条信息。如果对客户隐藏Department,可以减少耦合(coupling)。为了这一目的,我在Person中建立一个简单的委托函数:
public Person getManager() {
return _department.getManager();
}
现在,我得修改Person的所有客户,让它们改用新函数:
manager = john.getManager();
只要完成了对Department所有函数的委托关系,并相应修改了 Person的所有客户,我就可以移除Person中的访问函数个体Department()了。