Los Techies : Blogs about software and anything tech!

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.

Kick It on DotNetKicks.com
Posted Aug 28 2009, 12:38 PM by schambers

Comments

on 08-31-2009 11:56 AM

Agile/ALT.NET/Software Design A guide into OR/M implementation challenges: The Session Level Cache Build Your Own Data Access Layer: Lazy Loading A guide into OR/M implementation challenges: Laxy Loading Building Your Own Data Access Layer: Executing

31 Days of Refactoring « Vincent Leung's .NET Tech Clips wrote 31 Days of Refactoring « Vincent Leung's .NET Tech Clips
on 10-28-2009 9:28 AM

Pingback from  31 Days of Refactoring « Vincent Leung's .NET Tech Clips

张荣华 wrote 31天重构指南之二十九:去除中间人对象
on 11-05-2009 10:25 PM

今天要说的重构来自于Folwers的重构目录,你可以在这里查看。 有时在你的代码会存在一些幽灵类,Fowler称它们为“中间人”,中间人类除了调用别的对象之外不做任何事情,所以中间人类没有存在的必要...

PetterLiu wrote 31 Days of Refactoring
on 11-27-2009 4:02 AM

Refactoring Day 1 : Encapsulate Collection Refactoring Day 2 : Move Method Refactoring Day 3 : Pull ...

PetterLiu wrote 31 Days of Refactoring
on 11-27-2009 4:04 AM

Refactoring Day 1 : Encapsulate Collection Refactoring Day 2 : Move Method Refactoring Day 3 : Pull ...

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems