Skip to content

Middle Man

A Middle Man is a class whose methods mostly forward calls to another object, adding a layer of indirection with no behaviour of its own.

7 min readCode smellCoupling

A Middle Man is a class that does almost nothing itself. Most of its methods take the arguments they receive, pass them to another object, and return the result unchanged. Callers go through it, but they would get the same outcome by talking to the real object directly. This usually starts with a good intention. Someone hides a delegate to keep callers from reaching through a chain of objects, or wraps a dependency in case it changes later. Over time, every new feature on the inner class gets a matching pass-through method on the wrapper. The wrapper grows, but it never earns its keep. In an interview, this smell comes up when you review code with many thin service classes. Spotting it shows you can tell useful encapsulation from indirection that only adds cost.

Signs

How to spot it in a review.

  • Most public methods on the class are a single line that calls the same field and returns its result.
  • Adding a feature to the inner class always means adding a matching forwarding method to the wrapper.
  • Method names on the wrapper only restate the delegate's names, such as getAppointment calling find.
  • The class adds no validation, caching, logging, authorization or translation around the calls it forwards.
  • Tests for the wrapper only check that each call reached the delegate's mock with the same arguments.

What it looks like

A typical case, in three languages.

type AppointmentStatus = "booked" | "cancelled";

interface Appointment {
  id: string;
  patientId: string;
  clinicianId: string;
  startsAt: Date;
  status: AppointmentStatus;
}

class AppointmentBook {
  private readonly appointments = new Map<string, Appointment>();

  book(appt: Appointment): void {
    if (this.appointments.has(appt.id)) {
      throw new Error(`Duplicate appointment ${appt.id}`);
    }
    this.appointments.set(appt.id, appt);
  }

  cancel(id: string): void {
    const appt = this.appointments.get(id);
    if (!appt) throw new Error(`Unknown appointment ${id}`);
    appt.status = "cancelled";
  }

  find(id: string): Appointment | undefined {
    return this.appointments.get(id);
  }

  upcomingFor(patientId: string, now: Date): Appointment[] {
    return [...this.appointments.values()]
      .filter((a) => a.patientId === patientId && a.status === "booked" && a.startsAt > now)
      .sort((a, b) => a.startsAt.getTime() - b.startsAt.getTime());
  }
}

// Middle Man: every method forwards to AppointmentBook unchanged.
class PatientScheduler {
  constructor(private readonly book: AppointmentBook) {}

  bookAppointment(appt: Appointment): void {
    this.book.book(appt);
  }

  cancelAppointment(id: string): void {
    this.book.cancel(id);
  }

  getAppointment(id: string): Appointment | undefined {
    return this.book.find(id);
  }

  getUpcoming(patientId: string, now: Date): Appointment[] {
    return this.book.upcomingFor(patientId, now);
  }
}

const scheduler = new PatientScheduler(new AppointmentBook());
scheduler.bookAppointment({ id: "A-101", patientId: "P-7", clinicianId: "C-2", startsAt: new Date("2030-03-04T09:00:00Z"), status: "booked" });
scheduler.bookAppointment({ id: "A-102", patientId: "P-7", clinicianId: "C-5", startsAt: new Date("2030-03-01T14:30:00Z"), status: "booked" });
scheduler.cancelAppointment("A-101");
console.log(scheduler.getUpcoming("P-7", new Date("2030-01-01T00:00:00Z")).map((a) => a.id)); // [ 'A-102' ]

In this clinic booking system, PatientScheduler has four methods and all four forward straight to AppointmentBook. It checks nothing, translates nothing and owns no state besides the reference. Every new query on the book, such as a clinician's daily list, would need another pass-through here. The fix is Remove Middle Man: let callers depend on AppointmentBook directly and delete PatientScheduler. If one forwarding method later gains real logic, such as a 24-hour cancellation rule, move that rule into the book or keep only that method.

Why it happens

How code ends up like this.

  • Each forwarding method is code you have to read, test and keep in sync with the delegate, without getting any behaviour back for it.
  • Changing a signature on the real class means changing it twice, and the two versions can drift apart in naming or defaults.
  • Readers have to jump through an extra layer to find where the work actually happens, which slows down debugging and code review.
  • Mock-heavy tests of a pure forwarder only confirm that a call happened. They give a false sense of coverage while testing no logic.

Treatment

The refactorings that cure it, and when to choose each.

Payoff

What you get once it's gone.

  • Fewer classes and methods to read, test and maintain.
  • Signature changes happen in one place instead of two.
  • Callers see the object that does the work, which makes behaviour easier to trace.
  • Tests target real logic instead of checking that one call reached another.

When to leave it

Sometimes it's not worth fixing.

  • The wrapper is a deliberate Facade, Proxy or Adapter that shields callers from a third-party library or a remote service you expect to swap.
  • The wrapper is a boundary between modules or teams, and keeping the inner class private is worth the extra methods.
  • Removing it would force callers to chain through several objects, which breaks the Law of Demeter more than the forwarding costs you.
  • The wrapper exists for a cross-cutting concern such as authorization, metrics or transactions that is about to be added, and the plan is concrete.

Common questions

Short answers to the usual doubts.

How is a Middle Man different from a Facade or a Proxy?

Intent and value. A Facade simplifies a subsystem by combining several calls or hiding several objects behind one interface. A Proxy adds access control, caching, laziness or remoting. A Middle Man forwards one-to-one to a single object and adds nothing. If you cannot name what the wrapper does beyond passing calls through, it is a Middle Man.

Doesn't Remove Middle Man contradict Hide Delegate?

They are opposite moves, and you use whichever fixes the current problem. Hide Delegate reduces coupling when callers reach through a chain of objects. Remove Middle Man reduces indirection when hiding has gone so far that the wrapper is mostly forwarding. The goal is a balance: hide structure callers should not depend on, and expose objects they legitimately work with.

How many forwarding methods make a class a Middle Man?

There is no fixed number. Look at the proportion. If more than half of a class's public methods are single-line delegations to the same field, and the rest are small, the class is mostly indirection. A class with one forwarding method and ten methods of real logic is fine.

Isn't the wrapper useful for mocking in tests?

Usually not. You can mock or fake the delegate directly, or put an interface on it if you need a seam. Keeping a whole forwarding class only to have something to mock doubles the surface area, and tests of the wrapper end up checking only that calls were passed along.

How should I talk about this smell in an interview?

Point to the evidence: list the methods that forward unchanged and the ones, if any, that add behaviour. Then propose the smallest fix, such as removing the wrapper or inlining specific methods, and say what would make you keep it, for example a real boundary around an external service. That shows you weigh indirection against coupling instead of deleting layers by reflex.

Next guide5 of 5 · Smells: couplingGlobal Mutable StateRead the next one