Skip to content

Remove Middle Man: Let Callers Talk to the Real Object

Learn how to remove a class that only forwards calls to a delegate, so callers use the real object directly, with TypeScript, Python and PHP examples.

7 min readRefactoring

Remove Middle Man is the reverse of Hide Delegate. A class has filled up with methods that do nothing except pass the call to an object it holds. You expose that object and let callers talk to it directly, then delete the forwarding methods one at a time. In interviews this often comes up as a follow-up to the Law of Demeter. The interviewer wants to see that you treat encapsulation as a trade-off. Hiding a delegate is worth it when it shields callers from change. It stops being worth it when every new feature on the delegate forces you to add yet another pass-through method.

The problem

A situation where the obvious design starts to hurt.

Picture a team chat product called Orbit. Each Workspace is on a Plan that defines the name, monthly price, seat limit and storage quota. Early on, someone hid the plan behind Workspace to keep callers from depending on it. Now Workspace has a forwarding method for every plan field: planName(), monthlyPriceCents(), seatLimit(), storageQuotaGb(), allowsSeats().

The billing team wants to add an annual price and a custom-domain flag to Plan. Each change means editing two classes, and the second edit is pure boilerplate. Workspace adds no rules, no checks and no translation. It is a middle man, and its size grows with every feature added to the class it wraps.

Before and after

The same code, before and after the change.

Before

class Plan {
  constructor(
    readonly name: string,
    readonly monthlyPriceCents: number,
    readonly seatLimit: number,
    readonly storageQuotaGb: number,
  ) {}

  allowsSeats(count: number): boolean {
    return count <= this.seatLimit;
  }
}

class Workspace {
  constructor(readonly slug: string, private readonly plan: Plan) {}

  planName(): string {
    return this.plan.name;
  }

  monthlyPriceCents(): number {
    return this.plan.monthlyPriceCents;
  }

  seatLimit(): number {
    return this.plan.seatLimit;
  }

  storageQuotaGb(): number {
    return this.plan.storageQuotaGb;
  }

  allowsSeats(count: number): boolean {
    return this.plan.allowsSeats(count);
  }
}

function invoiceLine(ws: Workspace): string {
  const price = (ws.monthlyPriceCents() / 100).toFixed(2);
  return `${ws.slug}: ${ws.planName()} ${price} USD`;
}

function canInvite(ws: Workspace, currentSeats: number): boolean {
  return ws.allowsSeats(currentSeats + 1);
}

const ws = new Workspace("orbit-labs", new Plan("Team", 4900, 10, 50));
console.log(invoiceLine(ws)); // orbit-labs: Team 49.00 USD
console.log(canInvite(ws, 9)); // true
console.log(canInvite(ws, 10)); // false
console.log(`${ws.storageQuotaGb()} GB of ${ws.seatLimit()} seats`); // 50 GB of 10 seats

After

class Plan {
  constructor(
    readonly name: string,
    readonly monthlyPriceCents: number,
    readonly seatLimit: number,
    readonly storageQuotaGb: number,
  ) {}

  allowsSeats(count: number): boolean {
    return count <= this.seatLimit;
  }
}

class Workspace {
  constructor(readonly slug: string, readonly plan: Plan) {}
}

function invoiceLine(ws: Workspace): string {
  const price = (ws.plan.monthlyPriceCents / 100).toFixed(2);
  return `${ws.slug}: ${ws.plan.name} ${price} USD`;
}

function canInvite(ws: Workspace, currentSeats: number): boolean {
  return ws.plan.allowsSeats(currentSeats + 1);
}

const ws = new Workspace("orbit-labs", new Plan("Team", 4900, 10, 50));
console.log(invoiceLine(ws)); // orbit-labs: Team 49.00 USD
console.log(canInvite(ws, 9)); // true
console.log(canInvite(ws, 10)); // false
console.log(`${ws.plan.storageQuotaGb} GB of ${ws.plan.seatLimit} seats`); // 50 GB of 10 seats

Why it's worth it

What the change buys you.

Every forwarding method in Workspace repeated a fact that Plan already owned. The plan's interface was effectively written twice, and the copy had to be kept in sync by hand. Exposing the plan through a read-only accessor removes that duplication. When billing adds annualPriceCents to Plan, callers can use it right away, and nobody touches Workspace.

The hiding was also not buying any protection. Callers already depended on every concept the plan has: name, price, seat limit, quota. They just reached them through a longer route. Encapsulation pays off when the wrapper enforces a rule, translates between models, or isolates callers from a delegate that is likely to be swapped out. None of that applied here.

The accessor is read-only and Plan is immutable in all three versions. Making the delegate visible therefore does not let callers change a workspace's plan behind its back. If Workspace later needs to own a plan-related rule, such as a per-workspace seat override, you can add that one method back. It would have a real reason to exist.

  • Adding a field or method to the delegate no longer requires a matching pass-through method on the wrapper.
  • The wrapper class shrinks to what it actually owns, which makes its real responsibilities easier to see.
  • Callers see the true source of each value, which makes navigation and debugging more direct.
  • Each step is mechanical and can be done one method at a time without a big-bang change.

Careful with

  • You trade the middle man for longer call chains like ws.plan.seatLimit. If callers start reaching three or four objects deep, you have swapped one smell for message chains. Hide Delegate is the counter-move.
  • Expose the delegate read-only. A public mutable field lets any caller replace or mutate it, which is a bigger problem than the boilerplate you removed.
  • If the wrapper is part of a published API, removing methods is a breaking change. Deprecate them first and remove them in a later release.
  • You do not have to remove every forwarding method. Keep the ones that many callers use as a stable, meaningful operation of the wrapper itself.

Step by step

Small, safe steps, with the tests passing after each.

  1. Make sure tests cover the callers of each forwarding method, for example invoiceLine and canInvite, and that they pass.

  2. Add a read-only accessor for the delegate on the server class, such as a public readonly plan field or a plan property. Run the tests.

  3. Pick one forwarding method, for example planName(), and change one caller to use ws.plan.name instead. Run the tests.

  4. Repeat for the remaining callers of that method, then delete the forwarding method and let the compiler or a search confirm nothing still calls it. Run the tests.

  5. Do the same for monthlyPriceCents(), seatLimit(), storageQuotaGb() and allowsSeats(), one method per commit.

  6. Keep any method on the server class that adds real logic beyond forwarding, and review whether the class still deserves to exist.

Smells it removes

Smells this refactoring helps remove.

Common questions

Short answers to the usual doubts.

How do I decide between Hide Delegate and Remove Middle Man?

Look at how much the wrapper adds and how often the delegate changes. If the wrapper forwards most of the delegate's interface and each delegate change forces a wrapper change, remove the middle man. If callers only need one or two operations and you want freedom to swap the delegate, hide it. Teams often go back and forth as the code evolves, and that is fine.

Doesn't exposing the delegate violate the Law of Demeter?

Strictly, ws.plan.name talks to a stranger. The rule exists to limit coupling, though, and here callers were already coupled to every plan concept through the forwarding methods. Exposing an immutable value object adds very little coupling. Watch out for chains through mutable objects or chains that cross module boundaries.

What if the wrapper ends up empty after the refactoring?

Then ask whether it should exist at all. In the example, Workspace still holds its slug and is a natural home for future workspace rules, so it stays. If a class only held the delegate, you could replace it with the delegate itself, for example by inlining the class.

How would I explain this in an interview in one sentence?

When a class spends most of its methods forwarding calls to another object, give callers access to that object and delete the pass-throughs, so the interface is defined in one place instead of two.