public class Consumer {
public AccountManager AccountManager;//getter setter
public Consumer(AccountManager accountManager) {
AccountManager = accountManager;
}
public void Get(int id) {
Account account = AccountManager.GetAccount(id);
}
}
public class AccountManager {
public AccountDataProvider DataProvider;//getter setter
public AccountManager(AccountDataProvider dataProvider) {
DataProvider = dataProvider;
}
public Account GetAccount(int id) {
return DataProvider.GetAccount(id);
}
}
public class AccountDataProvider {
public Account GetAccount(int id) {
// get account
}
}
最终结果已经足够简单了。我们只需要移除中间人对象,将原始调用指向实际的接收者。
public class Consumer {
public AccountDataProvider AccountDataProvider;//getter settter
public Consumer(AccountDataProvider dataProvider) {
AccountDataProvider = dataProvider;
}
public void Get(int id) {
Account account = AccountDataProvider.GetAccount(id);
}
}
public class AccountDataProvider {
public Account GetAccount(int id) { // get account
}
}