What design patterns are, the common ones to name, how patterns affect performance, choosing a pattern, why connection pools (not singletons) manage database connections, and SOLID with examples.
Published September 25, 2026
Freshers aren't expected to recite all 23 Gang of Four patterns. Interviewers want three well-chosen examples, each tied to a real problem, and a clear explanation of SOLID. Your examples matter more than your definitions.
Short answer: A design pattern is a proven, reusable solution to a recurring design problem. It's a template for structuring classes and objects, not a piece of code to copy. Patterns give teams a shared vocabulary ("use a strategy here"), and they encode trade-offs that others have already worked out.
Key points to cover:
InputStream wrappers are Decorators.Runtime.getRuntime() is a Singleton.@Transactional works through a Proxy.JdbcTemplate uses Template Method.Learn it in depth → Singleton Pattern
Short answer: Pick patterns you can illustrate:
| Pattern | Problem it solves | Everyday example |
|---|---|---|
| Singleton | Exactly one shared instance | Configuration registry, Spring singleton beans |
| Factory Method | Create objects without hard-coding their class | PaymentGatewayFactory.forCountry("IN") |
| Builder | Many optional constructor parameters | HttpRequest.newBuilder().uri(...).build() |
| Observer | Notify many listeners when something changes | Spring ApplicationEvent listeners, UI events |
| Strategy | Swap algorithms at runtime | Pricing or discount strategies, Comparator |
| Decorator | Add behaviour without subclassing | new BufferedReader(new FileReader(f)) |
interface ShippingStrategy { BigDecimal cost(Order o); }
class StandardShipping implements ShippingStrategy { public BigDecimal cost(Order o) { return BigDecimal.valueOf(40); } }
class ExpressShipping implements ShippingStrategy { public BigDecimal cost(Order o) { return BigDecimal.valueOf(120); } }
// Checkout gets a ShippingStrategy injected: adding "same-day" means a new class, not a new if-branch
Learn it in depth → Strategy Pattern
Short answer: Usually only slightly. Patterns add some indirection (extra objects, interface calls), and the JIT compiler largely optimises that away. The real gains are maintainability and flexibility. Some patterns directly improve performance: Flyweight shares objects, Proxy enables lazy loading, and Object Pool reuses expensive resources.
Key points to cover:
Short answer: An Object Pool, meaning a connection pool such as HikariCP (the Spring Boot default). The pool keeps a set of open connections, lends one out for each unit of work, and takes it back afterwards. You avoid the high cost of opening a connection per request, and you cap the load on the database.
Common trap: answering "Singleton, one shared connection". A single JDBC connection can't safely serve concurrent requests: transactions from different threads would interfere, and it becomes a bottleneck. The pool object may be a singleton, but the connections are many.
Key points to cover:
Learn it in depth → Connection Pooling
Short answer: Start from the problem, not the pattern:
if/else over types, the need to add behaviour, a notification fan-out.Key points to cover:
switch on a type → Strategy or polymorphism.Short answer: Five object-oriented design principles that keep code easy to change:
VehicleRegistration class shouldn't also calculate insurance premiums.ServiceType implementation for electric vehicles), not by editing a tested switch.ElectricCar can't honour startEngine(), then the abstraction is wrong. Model start() on a Vehicle interface instead of forcing an engine onto every vehicle.VehicleOperations into Drivable, Refuelable, Chargeable and Navigable. An electric car implements Chargeable, not Refuelable.VehicleTracker talks to a GpsDevice interface, so any GPS hardware can be plugged in, and a fake one can be used in tests.// Dependency Inversion + Open/Closed together
interface GpsDevice { Position currentPosition(); }
class VehicleTracker {
private final GpsDevice gps; // abstraction, injected
VehicleTracker(GpsDevice gps) { this.gps = gps; }
Position locate() { return gps.currentPosition(); }
}
Key points to cover:
Learn it in depth → Single Responsibility & Open/Closed
Q: What's the difference between the Factory Method and Abstract Factory patterns?
A: Factory Method creates one product, and lets subclasses (or a single method) decide which concrete class to instantiate. Abstract Factory creates families of related products that must be used together, for example a UI toolkit's Button + Checkbox for Windows versus macOS.
Q: Is Singleton considered an anti-pattern? A: Hand-rolled global singletons often are. They hide dependencies, make unit testing hard, and create global mutable state. Container-managed singletons (Spring beans), injected through constructors, keep the benefit without those problems.
Q: Which SOLID principle does @Autowired constructor injection support?
A: Dependency Inversion. Your class declares the abstractions it needs, and the container supplies the concrete implementations.
Q: Give a real-world violation of the Single Responsibility Principle.
A: A "god" UserService that validates input, hashes passwords, writes to the database, sends emails and generates reports. Any change to email templates or database schemas forces edits to the same class, and its tests become huge.