November 26, 20268 minutes
Fourteen methods, twelve of them one-line forwards. Learn to spot a Middle Man, remove it, balance it against Hide Delegate, and tell it from Proxy, Decorator, Facade and Adapter.
Last lesson, in Message Chains, we hid every delegate behind a method. A month later, the box office class has fourteen methods. Twelve of them are a single line: each one passes the call straight to the venue. Then the venue team adds a feature, finding accessible seats, and the box office needs a thirteenth forward. A class that mostly passes calls along is a smell called Middle Man. It’s the other side of message chains.
Prefer to watch? The narrated video covers the same lesson (about 8 minutes). The article follows it section by section, with diagrams and a little extra depth.
Each forward passes the call straight to the venue: seatAt, sectionFor, openingTime, and nine
more. When the venue grows a new feature, the box office has to grow a matching forward. It’s what
happens when “hide the delegate” is applied everywhere without asking whether anyone needed the
hiding.
Picture a receptionist who answers every question the same way. She turns to the colleague beside her, and repeats your question word for word. The colleague answers, and she repeats the answer back to you. She never checks anything, decides anything, or adds anything. You could have asked the colleague yourself. All she added was a hop.
Compare a different receptionist. She checks your name, finds your booking, and hands you a map. That desk earns its place.
In code, the receptionist is the box office and the colleague is the venue. The course reference describes a middle man as a class that does little more than pass data or calls between two other classes. It doesn’t process, clean or convert what it handles.
Here’s the box office class. To be fair, it came from a good place. After last lesson, no screen reaches through it any more.
class BoxOffice {
private final Venue venue;
Seat seatAt(String section, int row, int number) {
return venue.seatAt(section, row, number);
}
String sectionFor(Seat seat) {
return venue.sectionFor(seat);
}
LocalTime openingTime() {
return venue.openingTime();
}
// nine more forwards, and refund()
Receipt sell(Ticket ticket, Payment payment) {
validate(ticket);
Receipt receipt = payment.charge(ticket.seatPrice());
log.recordSale(receipt);
return receipt;
}
}Each forward is correct. Each is a single line, with the right arguments, and it hides how the venue
works. Then there’s sell, which really does something: it validates the ticket, takes the payment
and writes the receipt to the log. So the class has twelve forwards and two methods that do real
work. That ratio is the clue.
seatAt, and the box office forward
needs it too.openingTime does, you open the box office, find a
forward, and open the venue: two files for one answer.LocalTime openingTime() { return venue.openingTime(); }A class earns its place by adding something: a rule, a check, a translation, or a simpler view. Strip
away the forwards, and ask what’s left. Here, it’s sell and refund. That’s the real class. If a
class only repeats what another class says, the other class can speak for itself.
The refactoring is called Remove Middle Man. It’s the inverse of Hide Delegate: clients stop going through the box office, and talk to the venue. Take the seat picker. It asks the box office for a seat, and the box office asks the venue:
class SeatPicker {
private final BoxOffice boxOffice;
Seat pick(String section, int row, int number) {
Seat seat = boxOffice.seatAt(section, row, number);
if (seat.isTaken()) {
throw new IllegalStateException("seat taken");
}
return seat;
}
}Give it the venue directly. The call changes, and nothing else about the seat picker does:
class SeatPicker {
private final Venue venue;
Seat pick(String section, int row, int number) {
Seat seat = venue.seatAt(section, row, number);
if (seat.isTaken()) {
throw new IllegalStateException("seat taken");
}
return seat;
}
}Do that for each client, and a forward loses its last caller. Delete it. When a forward has one caller left, inline the method instead: replace the call with the forward’s body.
// before
LocalTime opens = boxOffice.openingTime();
// after
LocalTime opens = venue.openingTime();Now the box office is what it should have been: sell and refund, and nothing else.
class BoxOffice {
Receipt sell(Ticket ticket, Payment payment) {
validate(ticket);
Receipt receipt = payment.charge(ticket.seatPrice());
log.recordSale(receipt);
return receipt;
}
void refund(Ticket ticket) {
ticket.refund();
log.recordRefund(ticket);
}
}Twelve forwards deleted, nothing lost, and the venue can grow without anyone editing the box office.
Wait. Last lesson said to hide delegates. This lesson says to remove the middle man. Which is it? Both are right, and they’re opposite moves. Hide Delegate takes a class’s insides out of the client’s sight. Remove Middle Man puts them back in view. In Fowler’s catalog each is the inverse of the other.
Push too far one way, and clients walk long chains. Push too far the other way, and a class is nothing but forwards.
The right point is in between. Forward what clients really ask: what does my seat cost?
(ticket.seatPrice().) Don’t forward everything the venue can do. So hide a delegate when many
clients keep reaching through it for the same question, and remove the middle man when forwards
outnumber real work.
A layered design often grows a ticket service above the ticket repository:
class TicketService {
private final TicketRepository repository;
Ticket find(long id) { return repository.find(id); }
void save(Ticket ticket) { repository.save(ticket); }
void delete(Ticket ticket) { repository.delete(ticket); }
}Find, save, delete. Each method passes straight through to the repository, with the same name and the same argument. Callers could hold the repository themselves. The service adds a layer of boxes, and no behavior.
Now compare a service that earns it. Cancelling a ticket checks the refund policy, deletes the ticket, and emails the customer:
void cancel(long id) {
Ticket ticket = repository.find(id);
policy.checkRefundable(ticket);
repository.delete(ticket);
mailer.sendCancellation(ticket);
}That’s the difference. The first class forwards. The second class decides.
Not all forwarding is a smell. Several patterns forward calls on purpose, and each adds something:
| Pattern | Forwards, and also | Lesson |
|---|---|---|
| Proxy | controls access, or delays the work | Proxy |
| Decorator | adds behavior around the call | Decorator |
| Facade | offers one simpler interface over many classes | Facade |
| Adapter | translates between two interfaces | Adapter |
| Middle Man | nothing | this lesson |
Each forward there has a reason you can name. A middle man has no reason to name. The course reference says the same: leave a class alone when it follows a design pattern such as Proxy or Decorator, where delegation is intentional.
One myth to drop: delegation is bad. It isn’t. Delegation is how objects cooperate. The empty kind is the smell.
And there is a reason to keep a forward. Remember the event’s seatPrice method from last lesson?
class Event {
private final Venue venue;
double seatPrice(String section, int row, int number) {
return venue.seatPrice(section, row, number);
}
}It forwards to the venue. But it keeps the ticket from knowing the venue exists. The course reference lists this as a case to ignore: an intermediary that reduces unnecessary coupling between two classes. That forward stays. The test is whether it hides something worth hiding, or only repeats.
So ask of every forward: what does this add? If the answer is nothing, remove it. If the answer is distance from a detail, keep it.
A middle man is a class whose methods mostly just pass calls along. Remove Middle Man, so clients talk to the delegate directly, and inline the few forwards that remain. Keep forwards that add something: a proxy, a decorator, a facade, an adapter, or real distance from a detail. Hide Delegate and Remove Middle Man are opposite moves, so stay somewhere between them.
Checklist:
Forward with a purpose, or don’t forward.