Remove Middle Man · Los Techies
28 August, 2009. It was a Friday.
Today’s refactoring comes from Fowler’s refactoring catalog and can be found here.
Sometimes in code you may have a set of “Phantom” or “Ghost” classes. Fowler calls these “Middle Men”. Middle Men classes simply take calls and forward them on to other components without doing any work. This is an unneeded layer and can be removed completely with minimal effort.
1: public class Consumer
2: {
3: public AccountManager AccountManager { get; set; }
4:
5: public Consumer(AccountManager accountManager)
6: {
7: AccountManager = accountManager;
8: }
9:
10: public void Get(int id)
11: {
12: Account account = AccountManager.GetAccount(id);
13: }
14: }
15:
16: public class AccountManager
17: {
18: public AccountDataProvider DataProvider { get; set; }
19:
20: public AccountManager(AccountDataProvider dataProvider)
21: {
22: DataProvider = dataProvider;
23: }
24:
25: public Account GetAccount(int id)
26: {
27: return DataProvider.GetAccount(id);
28: }
29: }
30:
31: public class AccountDataProvider
32: {
33: public Account GetAccount(int id)
34: {
35: // get account
36: }
37: }
</div> </div>
The end result is straightforward enough. We just remove the middle man object and point the original call to the intended receiver.
1: public class Consumer
2: {
3: public AccountDataProvider AccountDataProvider { get; set; }
4:
5: public Consumer(AccountDataProvider dataProvider)
6: {
7: AccountDataProvider = dataProvider;
8: }
9:
10: public void Get(int id)
11: {
12: Account account = AccountDataProvider.GetAccount(id);
13: }
14: }
15:
16: public class AccountDataProvider
17: {
18: public Account GetAccount(int id)
19: {
20: // get account
21: }
22: }
</div> </div>
This is part of the 31 Days of Refactoring series. For a full list of Refactorings please see the original introductory post.
网址:Remove Middle Man · Los Techies https://mxgxt.com/news/view/1680823
相关内容
How to Remove Display Flex in CSS?Replace conditional with Polymorphism · Los Techies
中国人middle name怎么填?英语中的“中间名”是什么意思?
Method Man
羅志祥,這樣的一個極品男人,叫我們怎么能不愛
Ye 在 ins 上分享了一段制作“Preacher Man”的视频:“PRODUCER MAN” Chop
乔杉扛起责任的模样真man,他的样子真的特别man,非常仗义
韩剧《秘行要员》(Man To Man)剧情简介 人物角色关系介绍
Running Man嘉宾列表
“詹姆斯”通常指的是美国职业篮球运动员勒布朗·詹姆斯(LeBron James),他目前效力于美国职业篮球联赛(NBA)洛杉矶湖人队(Los Angeles Lakers)。
随便看看
- 甄子丹沈阳参加活动遇尴尬,保安手牵手如临大敌,现场人少得可怜
- 出入机场的场面比国际巨星排场还大,请数十名保安
- 唐嫣现身某商场,动用几十名保安封锁现场,网友:谁给你的勇气?
- 18线男歌手商演耍大牌,30个保安霸两电梯护送,网友:退圈吧
- 历史求所知:“谁给你的特权?”青岛机场,网友在机场乘电梯时偶遇某“大明星”,竟被保安命令清空电梯,让大明星先走!网友当面怒怼:你谁啊?凭什么给你让?为什么让我们下来!网友:我今天就遇到了,说是明星来了,一位小鲜肉都不认识,让我们走货梯~ 随着互联网时代的发展,越来越多的明星走入到了大家的视野中,并且凭借着出众的外貌,深受粉丝的追捧,他们所到之处更是人头攒动,常常导致水泄不通影响正常秩序的情况。...

