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)。