When Not to Use a Design Pattern: Five Java Smells, the Patterns the Language Replaced, and a One-Page ADR for Saying No
Five tiny Java programs, each with a pattern that earns nothing. The same call prints 'invoice region eu', then 'us', because a singleton is global state. A factory returns an equal object to plain new. Five do-nothing decorators take the stack from 3 frames to 8. Then Visitor, Strategy and Builder replaced by a sealed switch, a lambda and a record.
What you will learn
- Recognise the golden hammer: a pattern applied because it is known, not because it is needed
- Spot five smells in small Java programs: a lone interface, a global singleton, a factory for one class, an empty decorator stack and an anemic model
- Replace Visitor, a one-line Strategy class and a Builder with a sealed switch, a lambda and a record constructor
- Ask five questions before adding a pattern and record the decision in a short ADR
Before you start
- Java interfaces, records and lambdas
- The Strategy and State patterns
Owning a router does not mean every joint should be a dovetail. A shelf needs two screws and two minutes. A drawer that is pulled hard every day earns the dovetail. Patterns work the same way. You choose one by looking at the problem in front of you, not at the toolbox. This article shows five places where a pattern costs more than it gives, each as a tiny Java program with its real output.
The golden hammer
Learn one good tool and everything starts to look like a nail. An if with two branches, a class with one job, a value that never changes, a loop over a list, one object made once, a single log line: each gets stamped "pattern". Most of them needed nothing at all.
Smell 1: an interface with one implementation
// The interface has one implementation and nobody plans a second.
interface TicketLister {
List<String> openTickets();
}
static class TicketListerImpl implements TicketLister {
@Override
public List<String> openTickets() {
return List.of("T-101", "T-102");
}
}
[T-101, T-102]
Two types to read for one behaviour, and the output is exactly what it would be without the interface. Use the class, and add the interface when a second implementation is real. A second one that exists only for a mock is not a reason to start with it.
Smell 2: a singleton used as global state
// A singleton used as global state: any class can change it from anywhere.
static class Settings {
static final Settings INSTANCE = new Settings();
String region = "eu";
}
static class Invoices {
String label() {
return "invoice region " + Settings.INSTANCE.region;
}
}
static class Reports {
void switchRegion() {
Settings.INSTANCE.region = "us";
}
}
invoice region eu
invoice region us
Same call, different answer, because another class changed the region from somewhere else. Pass the region into Invoices through its constructor and the surprise disappears.
Smell 3: a factory for one class
// A factory for a class with one shape and no choice to make.
static class TicketFactory {
static Ticket create(String id, String plan) {
return new Ticket(id, plan);
}
}
Ticket viaFactory = TicketFactory.create("T-101", "pro");
Ticket direct = new Ticket("T-101", "pro");
System.out.println(viaFactory.equals(direct));
true
The factory added a class and a call and changed nothing. Call new, and add a factory when the choice of class really varies.
Smell 4: a deep decorator stack
// Each wrapper adds nothing but one more call.
static class Wrap implements Desk {
private final Desk next;
Wrap(Desk next) {
this.next = next;
}
@Override
public String close(String id) {
return next.close(id);
}
}
Desk deep = new Wrap(new Wrap(new Wrap(new Wrap(new Wrap(new RealDesk())))));
RealDesk.close reports the stack depth it sees:
plain: closed T-101, stack depth 3
five wrappers: closed T-101, stack depth 8
Keep the wrappers that do real work, such as logging, caching or a retry, and remove the rest. (When a decorator does earn its place, it looks like decorators and proxies.)
Smell 5: an anemic model, by default
// Anemic: only getters and setters. Every rule has to live in some service, and it is easy to forget one.
static class AnemicTicket {
private String status = "OPEN";
private final List<String> comments = new ArrayList<>();
String getStatus() { ... }
void setStatus(String status) { ... }
List<String> getComments() { ... }
}
// Rich: the ticket guards its own rule.
static class Ticket {
private boolean closed;
private final List<String> comments = new ArrayList<>();
void close() {
closed = true;
}
void comment(String text) {
if (closed) {
throw new IllegalStateException("a closed ticket takes no comment");
}
comments.add(text);
}
}
anemic: CLOSED, comments 1
rich: a closed ticket takes no comment
The anemic ticket is closed and still accepts a comment. A rule that lives only in a service is skipped by the next caller that does not go through it. Let the object guard its own rule, as the sealed ticket states do in the State pattern article.
Patterns the language replaced
// Visitor, replaced: a sealed interface and a switch. The compiler checks every case.
sealed interface Shape permits Circle, Square {
}
record Circle(double radius) implements Shape {
}
record Square(double side) implements Shape {
}
static double area(Shape shape) {
return switch (shape) {
case Circle circle -> Math.PI * circle.radius() * circle.radius();
case Square square -> square.side() * square.side();
};
}
// Builder, replaced: a record with a second constructor for the defaults.
record Ticket(String id, String plan, boolean urgent) {
Ticket(String id) {
this(id, "basic", false);
}
}
// Strategy class, replaced: a lambda.
DoubleUnaryOperator fee = amount -> amount * 0.02;
area of Square 3 = 9.0
fee on 200 = 4.0
Ticket[id=T-1, plan=basic, urgent=false]
Add a Triangle to permits and the switch stops compiling until it has a case. The idea of each pattern is still there, and the ceremony moved into the language.
Five questions before you add a pattern
- What problem do I have today? Not someday.
- What is the simplest code that solves it?
- What does the pattern add: types, calls, names to learn?
- Does Java or Spring already do it?
- Would the next reader see why it is there?
Write the no down
An architecture decision record is useful for the pattern you added and for the one you did not. An example:
| Title | Fee rules stay one method for now |
|---|---|
| Status | Accepted |
| Context | Two plans, one rule each, and the rules change a few times a year |
| Decision | No Strategy classes yet. Revisit when a plan needs its own dependencies. |
| Consequences | One method to edit, less to read, and moving to Strategy later is a small change |
A no that is written down saves the next person the same debate. The format is covered in ADRs and C4 diagrams. A pattern has to earn its place, and the Strategy beans earned theirs only once a fourth plan arrived.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Java Classes and Objects: One Blueprint, Many Cars
Six loose variables for two cars doesn't scale. A Car class is the blueprint; new builds each car with its own brand, speed and fuel. Plus field defaults, and why two variables can point at one car.
Java Constructors and the this Keyword
A constructor decides how an object is born: new Car("Toyota", 42.5) arrives filled in. this.brand vs brand, a second constructor that calls this(brand, 50.0), and why new Car() stops compiling once you write your own.
Java Instance Methods, Getters, Setters and toString
Give a Car class behaviour: accelerate() changes only this car's speed, private fields are reached through getSpeed and setSpeed, and toString turns Car@1b6d3586 into 'Toyota doing 60 km/h'.
Java static: Fields, Methods and Blocks Shared by the Whole Class
A static field lives once on the class: every Car ticks the same carsBuilt counter while keeping its own brand. Static methods need no object, a static block runs before main, and main is static so the JVM can start with no object.
