Middle Man


Symptoms

  • Most methods of a class call the same or a similar method on another object:

     f() {delegate.f();} 

    (A class that mostly delegates its work is known as a middle man .)

Causes

For one thing, this can result from applying Hide Delegate to address Message Chains. Perhaps other features have moved out since then, and you're left with mostly delegating methods.

What to Do

  • In general, Remove Middle Man by having the client call the delegate directly.

  • If the delegate is owned by the middle man or is immutable, and the middle man has behavior to add, and the middle man can be seen as an example of the delegate, you might Replace Delegation with Inheritance .

Payoff

Reduces size. May improve communication.

Contraindications

  • Some patterns (e.g., Proxy or Decorator) intentionally create delegates. Don't remove a Middle Man that's there for a reason.

  • Middle Man and Message Chain trade off against each other.

  • Delegates provide a sort of fa §ade, letting a caller remain unaware of details of messages and structures. Removing a Middle Man can expose clients to more information than they should know.



Refactoring Workbook
Refactoring Workbook
ISBN: 0321109295
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net