Remove Middle Man

Directly access the delegate if the server is just passing messages.

“A class has too many methods that simply delegate to other objects.”

Reverse of Hide Delegate.

graph LR
    A[Client] -->|Calls| C[Delegate]

When to apply (Smells)

  • Middle Man: A class that does nothing but delegate work.

Motivation

  • Simplicity: If a class adds no value other than forwarding, removing it simplifies the design.

Mechanics (Steps)

  1. Create an accessor for the delegate.
  2. Replace delegate method calls with direct calls to the delegate.

Explanation

Problem

Class A has method x() which calls b.x(). Class A is just a “Middle Man”.

Solution

Let the client call b.x() directly.

Pros and Cons

Pros Cons
  • Less boilerplate
  • Higher coupling (Client knows Delegate)
  • Comparison

    • Inverse Refactoring: Hide Delegate.

    Code example

    Typescript

    Before

    After

    PHP

    Before

    After