有时你的代码里可能会存在一些“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. Apache Karaf配置远程调试

    软件环境 apache-karaf-4.0.0 配置方法: 在 bin/karaf.bat 文件里,顶部增加 set KARAF_DEBUG=true 然后.重新启动karaf 启动之后.就可以看到例 ...

  2. Spring中AOP的使用

    问题:什么是AOP? 答:AOP基本概念:Aspect-Oriented Programming,面向方面编程的简称,Aspect是一种新的模块化机制.用来描写叙述分散在对象.类或方法中的横切关注点( ...

  3. lineage 世系 血缘 容错机制 DAG

    当某个RDD的部分数据丢失时候,Saprk会根据记录的世系关系找到该RDD的父RDD以及更上级的RDD.只需要将该RDD依赖的上级RDD重新计算就可以将该RDD进行恢复. Directed Acycl ...

  4. socket.io中文文档

    socket.io 中文文档转载于:http://www.cnblogs.com/xiezhengcai/p/3956401.html 服务端 io.on(‘connection’,function( ...

  5. POJ3159 Candies —— 差分约束 spfa

    题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submiss ...

  6. [转]POI读写Excel 修改

    [转]POI读写Excel 修改 一.Excel基础 二.HSSF概况 三.通过usermodel读取文件 四.通过usermodel写入文件 五.通过eventusermodel读取文件 六.HSS ...

  7. JSTL1.1函数标签库(functions)如fn:length

    JSTL1.1函数标签库(functions)如fn:length 分类: java ssh22012-06-11 09:02 313人阅读 评论(1) 收藏 举报 stringcharacterxm ...

  8. 工作中常用到的JS校验

    1. // 验证是否为空 2. function check_blank(obj, obj_name){ 3. if(obj.value != ''){ 4. return true; 5. }els ...

  9. Java对象与对象引用变量的理解

    Java对象及对象引用 首先定义一个简单的类: class User{ int userId; String userName; } 我们在创建对象时,通常会写: User user = new Us ...

  10. I.MX6 DNS 查看、修改方法

    /************************************************************************** * I.MX6 DNS 查看.修改方法 * 说明 ...