有时你的代码里可能会存在一些“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. 一个DIV相对于另一个DIV定位

    <div style="position:relative"><div style="position:absolute; top:0px; left: ...

  2. Educational Codeforces Round 17 D. Maximum path DP

    题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using na ...

  3. C编程中fread 、fwrite 用法总结

    在C语言中进行文件操作时,我们经常用到fread()和fwrite(),用它们来对文件进行读写操作.下面详细绍一下这两个函数的用法.   我们在用C语言编写程序时,一般使用标准文件系统,即缓冲文件系统 ...

  4. Django's CSRF mechanism

    Forbidden (403) CSRF verification failed. Request aborted. You are seeing this message because this ...

  5. 包管理 import debug 模块管理 module

    import sys, os this_file_abspath = os.path.dirname(os.path.abspath(__file__)) ProjectUtil_path = '{} ...

  6. YTU 2555: 老大的烦恼

    2555: 老大的烦恼 时间限制: 1 Sec  内存限制: 128 MB 提交: 176  解决: 47 题目描述 万恶的小黑,布置了一道题给老大做:给你一个n位的数,现在要求 你随意删除m位后,任 ...

  7. UVA - 1401 Remember the Word(trie+dp)

    1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...

  8. CAShapeLayer和贝塞尔曲线配合使用

    前言 CAShapeLayer继承自CALayer,因此,可使用CALayer的所有属性.但是,CAShapeLayer需要和贝塞尔曲线配合使用才有意义. 关于UIBezierPath,请阅读文章:i ...

  9. C语言算法

    选择排序法:用第一个数分别和后面的数比较 冒泡排序法:相邻的两个数比较 01.单词首字母大写&统计单词个数 02: 编写一个函数int pieAdd(int n),计算1!+2!+3!+……+ ...

  10. pymemcache get start

    Getting started! A comprehensive, fast, pure-Python memcached client library. Basic Usage from pymem ...