Anti-patterns — what they are and common examples
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
An anti-pattern is a recurring solution that looks convenient but tends to create a known problem in its context. A God Object, for example, mixes unrelated responsibilities so every change collides in one class. A Golden Hammer uses the same framework or pattern for problems it does not fit. Copy-paste becomes dangerous when several copies represent one business rule and must stay synchronized; copying is not automatically wrong in every case. I explain the concrete harm—such as coupling, hidden control flow, or change risk—and the better direction, instead of using the label as a verdict.
Anti-patterns — what they are and common examples
Direct Answer
An anti-pattern is a recurring approach that looks attractive in some context but repeatedly causes known problems, especially when a better response is available. Examples include a God Object with too many responsibilities, a Golden Hammer used for every problem, and copy-paste changes that create copies which must evolve together. The label should explain the context and harm, not replace analysis.
Code Example
// ANTI-PATTERN: God Class - one class doing everything
@Service
public class CartGodClass {
// handles HTTP, business logic, database, email, logging...
public void processCheckout(HttpServletRequest request) {
var cartId = Long.parseLong(request.getParameter("id"));
// database access
var cart = jdbcTemplate.queryForObject("SELECT * FROM carts WHERE id = ?", cartId);
// payment processing
paymentGateway.charge(cart.getTotal());
// email sending
emailService.sendConfirmation(cart.getUserEmail());
// logging
System.out.println("Processed cart: " + cartId);
// all in one class!
}
}
// ANTI-PATTERN: Magic Numbers
public void expireOldCarts() {
var cutoff = Instant.now().minus(86400, ChronoUnit.SECONDS); // what is 86400?
// should be: var cutoff = Instant.now().minus(1, ChronoUnit.DAYS);
}
// ANTI-PATTERN: Copy-Paste Programming
public double calculateAdminDiscount(double price) {
return price * 0.9; // 10% off
}
public double calculateVipDiscount(double price) {
return price * 0.8; // 20% off - duplicated structure
}
Common Mistakes & Pitfalls
- Not knowing the term anti-pattern: In design discussions and code reviews, terms like God Class and Spaghetti Code are common vocabulary — not knowing them makes you seem inexperienced.
- Defending anti-patterns: Freshers sometimes say 'but my God Class works' without understanding that 'works now' doesn't mean 'maintainable later' — anti-patterns create technical debt.
- Overusing patterns (Golden Hammer): Ironically, trying to avoid anti-patterns by using design patterns everywhere creates a different anti-pattern — know when simple code is better than a pattern.
Check your understanding
Answer these without looking back at the article.
- What is the difference between a design pattern and an anti-pattern?
- How do you identify a God Class in a codebase?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.