徐州软件开发网、徐州程序员、徐州程序员招聘求职、徐州软件企业推荐、 软件项目交易。点击注册加入我们,进入论坛发布消息

【行动胜于雄辩】徐州程序员交流QQ群:324379(一群,满)1182709(二群).进群请先看公告! 招聘求职请先在本站'招聘\求职区'发布,只在群里发布的,将被请出程序员交流群! 谢谢合作

本主题共有 0 篇回复,最新回复发表于 10-10-2009 22:21,作者 xzsdn_08
页 1 / 1 (1 项)
帖子排序: 上一主题 下一主题
  • 10-10-2009 22:21

    • xzsdn_08
    • 灌水10强
      男
    • 注册时间 01-04-2009
    • 江苏徐州
    • 发帖总数 641

    31天重构指南 29去除中间人对象

    Refactoring Day 29 : Remove Middle Man

    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: }

    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: }

    This is part of the 31 Days of Refactoring series. For a full list of Refactorings please see the original introductory post.

页 1 / 1 (1 项)
徐州软件开发网、徐州软件开发社区、徐州程序员
© Copyright 2008-2011 XZSDN.NET   Powered By communityserver   站长博客 我的Blog
苏ICP备09002379号 | 意见反馈 | 联系我