Pattern Fever: When a Pattern Makes Code Worse
A pattern is a fix for a problem, so a pattern with no problem to fix (a factory for a factory, a strategy with one choice, an interface with one class that will never have another) is pure cost: more classes and more jumps for no change made easier; the cure is the simplest thing that works, and the pattern comes back when its problem does.
Journeyman · Software Development · OOP · Updated
After this lesson you can
- Spot pattern fever: a pattern with no problem to solve today, such as a factory for a factory or a strategy with one choice
- Remove a pattern that doesn't earn its keep, keeping the tests green
Key takeaways
- Pattern fever is adding patterns for problems the code doesn't have: a factory for a factory, a strategy with one choice, an interface with one class.
- Every pattern has a cost (more classes, more jumps to follow) that only a real change pays back.
- The test for any pattern: what change does it make easy, and is that change happening? If the answer is "someday", take it out.
- "You aren't going to need it" (YAGNI): build the simplest thing that works today, and add the pattern when its problem actually arrives.
Before this: Simple Factory: One Place That Says new, Strategy: When the Rule Needs a Class of Its Own, and Refactoring Toward a Pattern, whose small steps this lesson runs the other way.
Every lesson in this course has added a pattern. This one takes them out. Right before going on vacation, a teammate at Summit Gym "made the daily schedule printer flexible". Now the owner wants each line to show how long the class is, and it takes you twenty minutes to find where a line is built. The code works. It's much bigger than its job.
The symptom: patterns with nothing to do
Here is the schedule printer. It prints the day's classes in order of their start time:
List<GymClass> classes = new List<GymClass> { new GymClass(18, "HIIT", "Main floor"), new GymClass(7, "Yoga", "Studio A"), new GymClass(12, "Spin", "Bike room") };
new SchedulePrinter(new FormatterFactoryProvider(), new ByStartTime()).PrintDay(classes);
record GymClass(int Hour, string Kind, string Room);
interface ILineFormatter { string Format(GymClass gymClass); }
class LineFormatter : ILineFormatter { public string Format(GymClass gymClass) => quot;{gymClass.Hour}:00 {gymClass.Kind}, {gymClass.Room}"; }
// A factory that always makes the same formatter, and a factory for the factory.
class FormatterFactory { public ILineFormatter CreateFormatter() => new LineFormatter(); }
class FormatterFactoryProvider { public FormatterFactory GetFactory() => new FormatterFactory(); }
// A strategy with one choice.
interface ISortStrategy { List<GymClass> Sort(List<GymClass> classes); }
class ByStartTime : ISortStrategy { public List<GymClass> Sort(List<GymClass> classes) => classes.OrderBy(gymClass => gymClass.Hour).ToList(); }
class SchedulePrinter
{
private readonly FormatterFactoryProvider provider;
private readonly ISortStrategy sortStrategy;
public SchedulePrinter(FormatterFactoryProvider provider, ISortStrategy sortStrategy) { this.provider = provider; this.sortStrategy = sortStrategy; }
public void PrintDay(List<GymClass> classes)
{
ILineFormatter formatter = provider.GetFactory().CreateFormatter();
foreach (GymClass gymClass in sortStrategy.Sort(classes))
{
Console.WriteLine(formatter.Format(gymClass));
}
}
}
7:00 Yoga, Studio A
12:00 Spin, Bike room
18:00 HIIT, Main floor
record GymClass(int hour, String kind, String room) {}
interface LineFormatter { String format(GymClass gymClass); }
class PlainLineFormatter implements LineFormatter { public String format(GymClass gymClass) { return gymClass.hour() + ":00 " + gymClass.kind() + ", " + gymClass.room(); } }
// A factory that always makes the same formatter, and a factory for the factory.
class FormatterFactory { LineFormatter createFormatter() { return new PlainLineFormatter(); } }
class FormatterFactoryProvider { FormatterFactory getFactory() { return new FormatterFactory(); } }
// A strategy with one choice.
interface SortStrategy { List<GymClass> sort(List<GymClass> classes); }
class ByStartTime implements SortStrategy {
public List<GymClass> sort(List<GymClass> classes) { return classes.stream().sorted(Comparator.comparingInt(GymClass::hour)).toList(); }
}
class SchedulePrinter {
private final FormatterFactoryProvider provider;
private final SortStrategy sortStrategy;
SchedulePrinter(FormatterFactoryProvider provider, SortStrategy sortStrategy) { this.provider = provider; this.sortStrategy = sortStrategy; }
void printDay(List<GymClass> classes) {
LineFormatter formatter = provider.getFactory().createFormatter();
for (GymClass gymClass : sortStrategy.sort(classes)) {
IO.println(formatter.format(gymClass));
}
}
}
void main() {
List<GymClass> classes = List.of(new GymClass(18, "HIIT", "Main floor"), new GymClass(7, "Yoga", "Studio A"), new GymClass(12, "Spin", "Bike room"));
new SchedulePrinter(new FormatterFactoryProvider(), new ByStartTime()).printDay(classes);
}
7:00 Yoga, Studio A
12:00 Spin, Bike room
18:00 HIIT, Main floor
class LineFormatter:
def format(self, gym_class):
hour, kind, room = gym_class
return f"{hour}:00 {kind}, {room}"
class FormatterFactory:
"""A factory that always makes the same formatter."""
def create_formatter(self):
return LineFormatter()
class FormatterFactoryProvider:
"""A factory for the factory."""
def get_factory(self):
return FormatterFactory()
class ByStartTime:
"""A strategy with one choice."""
def sort(self, classes):
return sorted(classes)
class SchedulePrinter:
def __init__(self, provider, sort_strategy):
self.provider = provider
self.sort_strategy = sort_strategy
def print_day(self, classes):
formatter = self.provider.get_factory().create_formatter()
for gym_class in self.sort_strategy.sort(classes):
print(formatter.format(gym_class))
classes = [(18, "HIIT", "Main floor"), (7, "Yoga", "Studio A"), (12, "Spin", "Bike room")]
SchedulePrinter(FormatterFactoryProvider(), ByStartTime()).print_day(classes)
7:00 Yoga, Studio A
12:00 Spin, Bike room
18:00 HIIT, Main floor
class LineFormatter {
format(gymClass) { return `${gymClass.hour}:00 ${gymClass.kind}, ${gymClass.room}`; }
}
// A factory that always makes the same formatter, and a factory for the factory.
class FormatterFactory { createFormatter() { return new LineFormatter(); } }
class FormatterFactoryProvider { getFactory() { return new FormatterFactory(); } }
// A strategy with one choice.
class ByStartTime {
sort(classes) { return [...classes].sort((a, b) => a.hour - b.hour); }
}
class SchedulePrinter {
#provider;
#sortStrategy;
constructor(provider, sortStrategy) { this.#provider = provider; this.#sortStrategy = sortStrategy; }
printDay(classes) {
const formatter = this.#provider.getFactory().createFormatter();
for (const gymClass of this.#sortStrategy.sort(classes)) {
console.log(formatter.format(gymClass));
}
}
}
const classes = [{ hour: 18, kind: "HIIT", room: "Main floor" }, { hour: 7, kind: "Yoga", room: "Studio A" }, { hour: 12, kind: "Spin", room: "Bike room" }];
new SchedulePrinter(new FormatterFactoryProvider(), new ByStartTime()).printDay(classes);
7:00 Yoga, Studio A
12:00 Spin, Bike room
18:00 HIIT, Main floor
interface GymClass { hour: number; kind: string; room: string; }
interface LineFormatter { format(gymClass: GymClass): string; }
class PlainLineFormatter implements LineFormatter {
format(gymClass: GymClass): string { return `${gymClass.hour}:00 ${gymClass.kind}, ${gymClass.room}`; }
}
// A factory that always makes the same formatter, and a factory for the factory.
class FormatterFactory { createFormatter(): LineFormatter { return new PlainLineFormatter(); } }
class FormatterFactoryProvider { getFactory(): FormatterFactory { return new FormatterFactory(); } }
// A strategy with one choice.
interface SortStrategy { sort(classes: GymClass[]): GymClass[]; }
class ByStartTime implements SortStrategy {
sort(classes: GymClass[]): GymClass[] { return [...classes].sort((a, b) => a.hour - b.hour); }
}
class SchedulePrinter {
private readonly provider: FormatterFactoryProvider;
private readonly sortStrategy: SortStrategy;
constructor(provider: FormatterFactoryProvider, sortStrategy: SortStrategy) { this.provider = provider; this.sortStrategy = sortStrategy; }
printDay(classes: GymClass[]): void {
const formatter = this.provider.getFactory().createFormatter();
for (const gymClass of this.sortStrategy.sort(classes)) {
console.log(formatter.format(gymClass));
}
}
}
const classes: GymClass[] = [{ hour: 18, kind: "HIIT", room: "Main floor" }, { hour: 7, kind: "Yoga", room: "Studio A" }, { hour: 12, kind: "Spin", room: "Bike room" }];
new SchedulePrinter(new FormatterFactoryProvider(), new ByStartTime()).printDay(classes);
7:00 Yoga, Studio A
12:00 Spin, Bike room
18:00 HIIT, Main floor
Three lines of output, and seven classes to make them. To change how a line looks, you follow PrintDay to the provider, the provider to the factory, the factory to the formatter, and there it is.
- The symptom: patterns with nothing to do. A factory that always makes the same thing, a factory whose only job is making that factory, a strategy with one choice, an interface with one class that will never have another. Books on smells call this speculative generality: code built for changes someone guessed might come.
- The cause: patterns added because they're patterns, not because a problem asked for them. It happens most often right after learning them, which is why it has a nickname, pattern fever, and the general name over-engineering: building more than the problem needs.

The test: what does this pattern make easy?
Every pattern in this course was a fix for a change: a new product every month, a new deal every week, a new carrier, a new device. A pattern pays for its extra classes by making that change easy. So ask of each one:
| The pattern here | What change would it make easy? | Is that change happening? |
|---|---|---|
FormatterFactoryProvider |
Swapping which factory is used | There's one factory, and nobody has asked for another |
FormatterFactory |
Choosing between formatters | It always makes the same one |
ByStartTime, a strategy |
Sorting in other orders | The schedule is always by start time |
| The formatter's interface | Plugging in other formatters | There's one formatter |
When every answer is "someday", the patterns are cost with no payback. The old advice for this is "you aren't going to need it", known by its letters, YAGNI: build the simplest thing that works for what's asked today.
The cure: the simplest thing that works
Take the patterns out with the same small steps as Refactoring Toward a Pattern, run the other way: move the formatting into the printer, run the tests; replace the provider and factory calls with the formatting itself, run the tests; delete the unused classes, run the tests. What's left:
List<GymClass> classes = new List<GymClass> { new GymClass(18, "HIIT", "Main floor"), new GymClass(7, "Yoga", "Studio A"), new GymClass(12, "Spin", "Bike room") };
PrintDay(classes);
void PrintDay(List<GymClass> classes)
{
foreach (GymClass gymClass in classes.OrderBy(gymClass => gymClass.Hour))
{
Console.WriteLine( quot;{gymClass.Hour}:00 {gymClass.Kind}, {gymClass.Room}");
}
}
record GymClass(int Hour, string Kind, string Room);
7:00 Yoga, Studio A
12:00 Spin, Bike room
18:00 HIIT, Main floor
record GymClass(int hour, String kind, String room) {}
void printDay(List<GymClass> classes) {
for (GymClass gymClass : classes.stream().sorted(Comparator.comparingInt(GymClass::hour)).toList()) {
IO.println(gymClass.hour() + ":00 " + gymClass.kind() + ", " + gymClass.room());
}
}
void main() {
printDay(List.of(new GymClass(18, "HIIT", "Main floor"), new GymClass(7, "Yoga", "Studio A"), new GymClass(12, "Spin", "Bike room")));
}
7:00 Yoga, Studio A
12:00 Spin, Bike room
18:00 HIIT, Main floor
def print_day(classes):
for hour, kind, room in sorted(classes):
print(f"{hour}:00 {kind}, {room}")
print_day([(18, "HIIT", "Main floor"), (7, "Yoga", "Studio A"), (12, "Spin", "Bike room")])
7:00 Yoga, Studio A
12:00 Spin, Bike room
18:00 HIIT, Main floor
function printDay(classes) {
for (const gymClass of [...classes].sort((a, b) => a.hour - b.hour)) {
console.log(`${gymClass.hour}:00 ${gymClass.kind}, ${gymClass.room}`);
}
}
printDay([{ hour: 18, kind: "HIIT", room: "Main floor" }, { hour: 7, kind: "Yoga", room: "Studio A" }, { hour: 12, kind: "Spin", room: "Bike room" }]);
7:00 Yoga, Studio A
12:00 Spin, Bike room
18:00 HIIT, Main floor
interface GymClass { hour: number; kind: string; room: string; }
function printDay(classes: GymClass[]): void {
for (const gymClass of [...classes].sort((a, b) => a.hour - b.hour)) {
console.log(`${gymClass.hour}:00 ${gymClass.kind}, ${gymClass.room}`);
}
}
printDay([{ hour: 18, kind: "HIIT", room: "Main floor" }, { hour: 7, kind: "Yoga", room: "Studio A" }, { hour: 12, kind: "Spin", room: "Bike room" }]);
7:00 Yoga, Studio A
12:00 Spin, Bike room
18:00 HIIT, Main floor
The same three lines, from one short function. The owner's change, adding each class's length, is now one edit, and you can see it from where the schedule is printed.
When the pattern should come back
Taking a pattern out isn't a vow never to use it. If the phone app really does need a shorter line next month, that's a second way to format, and a formatter handed in (Strategy, in its function form) starts to earn its place. If a third way arrives, you'll be glad of it. The next chapter, When Repetition Becomes a Pattern: The Rule of Three, is about exactly that moment.
The opposite mistake is real too: refusing every pattern, and copying the same switch into a fifth file. The question is always the same one: what change is this making easy, and is that change happening?
How often you'll meet it: often, in codebases written by people who'd learned patterns, including, at some point, your own (our estimate).
Check yourself
2 questions to try on the lesson page.
So what
A pattern is an answer to a problem. Without the problem, it's more code to read. Ask what change each pattern makes easy, take out the ones whose change isn't coming, and bring them back when it does.