Skip to the guide

Telling look-alike patterns apart: strategy vs state, decorator vs proxy, factory vs builder

IntermediateConcept7 pages

Some patterns arrange their classes the same way, or get mixed up because they do similar jobs. Interviewers ask "isn't that just a Strategy?" to learn whether you know why a pattern exists, and they want a short reason, not a recital of its classes. This page gives you one question for each of four pairs: the title's three and adapter vs facade.

Two people in one uniform

Picture the front door of an office building, with two people in the same uniform. One of them greets every visitor, writes each name in a book and sends everyone upstairs. The other checks each question against a list he keeps. Sometimes he answers "she's out today" from his list, and nobody upstairs ever hears about it. In a photo you can't tell them apart, so you would ask one thing: does everyone still go up, or do you decide?

In code, the uniform is a class's shape: it implements an interface and holds an object of the same type. The greeter is a decorator, Logged, and the one with the list is a proxy, CachingCatalog. Upstairs is the real object, RemoteCatalog. The photo is the class diagram, and the question you would ask is what you need for each pair.