Skip to main content

Object-Oriented Sandwiches – Part 2

Nov 2012 - Grand Rapids

Welcome to part two of my series explaining Object-Oriented Design (OOD) through the metaphor of a fictional sandwich shop. In this part we’ll examine communication, the importance of trust and why everybody hates a micromanager.

These examples build on the previous post, so if you haven’t read it yet, check out part one first.

Welcome back to the Rubytown Sandwich Shop

We’ve discovered that the sandwich shop is not just a single thing, it’s a collection of things: sandwich-makers and sandwiches, recipes and rooms. But that could just as easily describe a bankrupt business, with its windows shuttered and its doors locked. A thriving business like RSS requires these objects to do things. To interact. To communicate.

How to order a sandwich

Ordering a sandwich requires communication between the customer, Jane and Bob. It looks like this:

Take a moment to notice what’s not being passed:

When Jane orders a sandwich from Bob, she hangs an order slip in the window. Jane knows the slip can hold a sandwich name, an amount, a tax amount and a grand total. All Bob cares about is that the slip will already contain a sandwich name: he ignores the rest. Bob and Jane use the slip for different purposes, but what’s on the order slip gives both of them everything they need.

In programming, communicating through a standard form with clearly-defined rules is called a “protocol” or a “contract”. Order slips are “data transfer objects” because we use them to transfer data – sandwich names, etc. – from one object to another.

The other benefit of this approach is that the exchanges are so simple: the customer tells Jane “I want” and Jane tells Bob “I want”. Jane and Bob each have a single call in their public interface, making it clear how they can interact. All the details of how sandwiches are made and how taxes figure into the final total are hidden from the other players.

It’s about trust.

How not to order a sandwich (a.k.a., nobody likes a micromanager)

Now let’s imagine a different world: a world where the customer doesn’t trust Bob and Jane. Our untrusting customer is named Richard. This is how Richard orders a sandwich.

In the real sandwich shop, this story would end with Bob chasing Richard down the street waving a bread knife, but believe it or not, this is how a lot of people write object-oriented programs. In this style, the “customer” object – which would be Richard – is making all the decisions and manipulating the other objects to accomplish the task. The other objects are merely extra sets of hands that know how to juggle or give a thumbs-up or tie a pair of shoelaces.

Without trust, your customer object becomes the most dreaded of bosses: the “micromanager”. Micromanagers don’t trust anyone but themselves to get the job done right, so they make sure the only things their employees do are so trivial and brutally-simple that there’s no chance of failure. Any sort of decision-making, problem-handling and result-verifying is too important to be entrusted to the low-level morons who work for them. Micromanagers operate under the principle that they are the only member of their team with a functioning brain and this disrespect makes it very hard to keep employees, who head for the hills at the first glimpse of daylight.

The practical downside is that the micromanager needs to know and be involved in every single detail about every single job his “team” might be expected to do. Any time the details of something change, he needs to know. If the toner runs low in the printer, he needs to tell someone to order more. If a supplier changes addresses, he needs to update his forms. Micromanagers are not calm, relaxed people. They also can’t ever get sick or go on vacation or even take a long lunch because their “team” is helpless when left to its own devices.

In programming, we call this a “dependency”: every member of Richard’s team depends on him for the details of how they do their job. If he is ever promoted, quits or gets run over by a bus, they are helpless. Highly-functional teams have dependencies that run in the other direction: the manager knows she can depend on her team to get work done with little to no supervision, because they’ve been trained to do a complete job, not just the trivial parts.

In addition, notice how complicated the exchanges have become. I count 7 different calls being made, all from Richard to either Jane or Bob. The order slip has disappeared and so have Bob and Jane’s private interfaces, because everything is now public. If any of those details change, the customer will have to change as well, increasing the hassle of maintenance and the opportunity for error.

So how do we get trustworthy “employees” to rely on? Bob and Jane are forced to investigate this in our next installment when Bob gets bad news from his doctor.

Takeaways