有时你的代码里可能会存在一些“Phantom”或“Ghost”类,Fowler称之为“中间人(Middle Man)”。这些中间人类仅仅简单地将调用委托给其他组件,除此之外没有任何功能。 这一层是完全没有必要的,我们可以不费吹灰之力将其完全移除。

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
}
}

重构29-Remove Middle Man(去掉中间人)的更多相关文章

  1. 重构改善既有代码设计--重构手法15:Remove Middle Man (移除中间人)

    某个类做了过多的简单委托动作.让客户直接调用受托类. 动机:在Hide Delegate (隐藏委托关系)的“动机”中,谈到了“封装委托对象”的好处.但是这层封装也是要付出代价的,它的代价是:每当客户 ...

  2. 『重构--改善既有代码的设计』读书笔记----Remove Middle Man

    如果你发现某个类做了过多的简单委托动作,你就可以考虑是否可以让客户直接去调用受托类.在Hide Delegate中,我们介绍了封装受托对象的好处,但好处归好处也存在代价,就是当你每次需要在受托对象中增 ...

  3. 重构第29天 去除中间人对象(Remove Middle Man)

    理解:本文中的”去除中间人对象”是指把 在中间关联而不起任何其他作用的类移除,让有关系的两个类直接进行交互. 详解:有些时候在我们的代码会存在一些”幽灵类“,设计模式大师Martin Fowler称它 ...

  4. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  5. [LeetCode] 402. Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  6. 重构24-Remove Arrowhead Antipattern(去掉箭头反模式)

    基于c2的wiki条目.Los Techies的Chris Missal同样也些了一篇关于反模式的post.  简单地说,当你使用大量的嵌套条件判断时,形成了箭头型的代码,这就是箭头反模式(arrow ...

  7. 重构26-Remove Double Negative(去掉双重否定)

    尽管我在很多代码中发现了这种严重降低可读性并往往传达错误意图的坏味道,但这种重构本身还是很容易实现的.这种毁灭性的代码所基于的假设导致了错误的代码编写习惯,并最终导致bug.如下例所示: public ...

  8. 重构27-Remove God Classes(去掉神类)

    在传统的代码库中,我们常常会看到一些违反了SRP原则的类.这些类通常以Utils或Manager结尾,有时也没有这么明显的特征而仅仅是普通的包含多个功能的类.这种God类还有一个特征,使用语句或注释将 ...

  9. 【Java重构系列】重构31式之封装集合

    2009年,Sean Chambers在其博客中发表了31 Days of Refactoring: Useful refactoring techniques you have to know系列文 ...

随机推荐

  1. 【iOS系列】-oc中的集合类

    OC中的集合有:NSArray 数组 NSDictionary 字典 NSSet 集合 第一:NSArrary 1.1:集合的基本方法 //1.创建集合 //NSArray 是不可变数组,一旦创建完成 ...

  2. Effective JavaScript Item 39 绝不要重用父类型中的属性名

    本系列作为Effective JavaScript的读书笔记. 假设须要向Item 38中的Actor对象加入一个ID信息: function Actor(scene, x, y) { this.sc ...

  3. unable to instantiate activity...

    Activity跳转到Activity,后来由于项目需要将第二个Activity改成继承FragmentActivity,跳转报错...无法初始化Activity,找不到class云云.. 最后是将b ...

  4. Django值中间件

    1,还是那句话:写代码的逻辑遵循:简洁,重复性高,可维护性高 1.1>中间件:中间件是一种用来处理Django的请求和响应的框架级别的钩子.它是一个轻量,低级别的插件系统,用于在全局范围内改变D ...

  5. Django值聚合,分组,事物,cookie,session

    1,聚合(aggregate):是queryset的一个 终止语句,它返回一个包含键值对的字典,键是的名称是聚合值的标识符,值是计算出来的聚合值,键的名称是按照字段和聚合函数自动生成出来的.用到的内置 ...

  6. JAVA程序员常用软件整理

    Java类软件:-------------------------------JDK7.0:http://pan.baidu.com/s/1jGFYvAYMyclipse8.5破解版:http://p ...

  7. (13)javaWeb中HttpServletRequest详解

    关于HTTP请求和响应,可以参考 HTTP协议 系列文章 导学,请求概述: a,GET和POST请求报文格式: b,常见的请求头 在servlet中,相应的doGet方法和doSet方法中的reque ...

  8. git unstage

    https://stackoverflow.com/questions/6919121/why-are-there-2-ways-to-unstage-a-file-in-git git rm --c ...

  9. AngularJS 指令(Directives)实践指南

    指令(Directives)是所有AngularJS应用最重要的部分.尽管AngularJS已经提供了非常丰富的指令,但还是经常需要创建应用特定的指令.这篇教程会为你讲述如何自定义指令,以及介绍如何在 ...

  10. bzoj2502【有上下界的最大流】

    2502: 清理雪道 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 834  Solved: 442[Submit][Status][Discuss] ...