Factory pattern — object creation without new
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
A factory moves object-construction decisions behind a method or component. The caller asks for a product interface, such as PaymentProcessor, and does not need to know the selected implementation's constructor or setup steps. The factory still creates the object internally; it does not remove new. This helps when the concrete type depends on input or configuration, or when creation has validation and resource setup. Adding a product may still require updating a registry or selection rule, so I do not claim automatic Open/Closed compliance. For a simple stable object, direct construction or normal dependency injection is easier to follow.
Factory pattern — object creation without new
Direct Answer
A factory moves a construction decision behind a method or object so callers depend on the product contract rather than construction details. The factory still uses constructors or another creation mechanism internally. It is useful when the concrete type varies, setup is complex, or lifecycle belongs to a separate component. Adding a product may still require updating registration or selection logic.
Common Mistakes & Pitfalls
- Calling any object-returning method a factory.
getUserById()is a lookup, not a factory. Factory means choosing a concrete type behind an interface. - Using Factory for everything, even simple value objects/DTOs — that's needless indirection.
new OrderItem(id, qty)is fine. - Returning the concrete type from the factory — return the interface, or callers stay coupled.
- Missing the 'why' — the value is adding types without editing client code (OCP), not just 'creating objects'.
Check your understanding
Answer these without looking back at the article.
- What is the difference between Simple Factory and Factory Method pattern?
- How does Spring use the Factory pattern?
- When is plain new() better than a Factory?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.