重构29-Remove Middle Man(去掉中间人)
有时你的代码里可能会存在一些“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(去掉中间人)的更多相关文章
- 重构改善既有代码设计--重构手法15:Remove Middle Man (移除中间人)
某个类做了过多的简单委托动作.让客户直接调用受托类. 动机:在Hide Delegate (隐藏委托关系)的“动机”中,谈到了“封装委托对象”的好处.但是这层封装也是要付出代价的,它的代价是:每当客户 ...
- 『重构--改善既有代码的设计』读书笔记----Remove Middle Man
如果你发现某个类做了过多的简单委托动作,你就可以考虑是否可以让客户直接去调用受托类.在Hide Delegate中,我们介绍了封装受托对象的好处,但好处归好处也存在代价,就是当你每次需要在受托对象中增 ...
- 重构第29天 去除中间人对象(Remove Middle Man)
理解:本文中的”去除中间人对象”是指把 在中间关联而不起任何其他作用的类移除,让有关系的两个类直接进行交互. 详解:有些时候在我们的代码会存在一些”幽灵类“,设计模式大师Martin Fowler称它 ...
- [LeetCode] Remove K Digits 去掉K位数字
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- [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 ...
- 重构24-Remove Arrowhead Antipattern(去掉箭头反模式)
基于c2的wiki条目.Los Techies的Chris Missal同样也些了一篇关于反模式的post. 简单地说,当你使用大量的嵌套条件判断时,形成了箭头型的代码,这就是箭头反模式(arrow ...
- 重构26-Remove Double Negative(去掉双重否定)
尽管我在很多代码中发现了这种严重降低可读性并往往传达错误意图的坏味道,但这种重构本身还是很容易实现的.这种毁灭性的代码所基于的假设导致了错误的代码编写习惯,并最终导致bug.如下例所示: public ...
- 重构27-Remove God Classes(去掉神类)
在传统的代码库中,我们常常会看到一些违反了SRP原则的类.这些类通常以Utils或Manager结尾,有时也没有这么明显的特征而仅仅是普通的包含多个功能的类.这种God类还有一个特征,使用语句或注释将 ...
- 【Java重构系列】重构31式之封装集合
2009年,Sean Chambers在其博客中发表了31 Days of Refactoring: Useful refactoring techniques you have to know系列文 ...
随机推荐
- Timus 1545. Hieroglyphs Trie的即学即用 实现字典提示功能
前面学了Trie,那么就即学即用.运用Trie数据结构来解决这道题目. 本题目比較简单,当然能够不使用Trie.只是多用高级数据结构还是非常有优点的. 题目: Vova is fond of anim ...
- C项目实践--贪吃蛇(1)
1.功能需求分析 1.1主要功能 i.游戏欢迎界面 ii.游戏执行功能,包括计算得分 iii.游戏结束界面 1.2游戏基本规则 游戏开始时蛇的长度是4个单位,并且按照当前方向不停地移动.移动范围是CO ...
- js来获取所有屏幕适配的总结
"屏幕分辨率为:"+screen.width+"*"+screen.height "屏幕可用大小:"+screen.availWidth+& ...
- POJ1236 Network of Schools —— 强连通分量 + 缩点 + 入出度
题目链接:http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Tot ...
- YTU 1003: Redraiment的遭遇
1003: Redraiment的遭遇 时间限制: 1000 Sec 内存限制: 128 MB 提交: 198 解决: 71 题目描述 Redraiment的老家住在工业区,日耗电量非常大.是政府 ...
- BZOJ_4184_shallot_线段树按时间分治维护线性基
BZOJ_4184_shallot_线段树按时间分治维护线性基 Description 小苗去市场上买了一捆小葱苗,她突然一时兴起,于是她在每颗小葱苗上写上一个数字,然后把小葱叫过来玩游戏. 每个时刻 ...
- bzoj 1127 KUP —— 最大子矩形+答案构造
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1127 首先,把权值 > 2*k 的点作为“坏点”,然后在图中用悬线法找权值最大的子矩形 ...
- 【转载】OAuth的机制原理讲解及开发流程
1.OAuth的简述 OAuth(Open Authorization,开放授权)是为用户资源的授权定义了一个安全.开放及简单的标准,第三方无需知道用户的账号及密码,就可获取到用户的授权信息,并且这是 ...
- ubuntu /etc/profile和/etc/environment的比较 (转载)
转自:http://blog.csdn.net/teamlet/article/details/8257853 先将export LANG=zh_CN加入/etc/profile ,退出系统重新登录, ...
- Ruby Time类和Date类
Time类 更新: 2017/06/23 更新了Data/Time在model模式下的便利方法 更新: 2018/10/12 修改了%Y相关描述防止误解 年月日时分秒,时区 生成 获取当前时 ...