Fundamentals · Hard · Free
Explain the Factory pattern with a simple API client example.
FresherFundamentalsOOP & Language Fundamentals
Factory pattern
A factory centralizes object creation so callers depend on an interface, not new ConcreteClient() scattered everywhere.
java
interface ApiClient { Response get(String path); }
class ApiClientFactory {
static ApiClient forEnv(String env) {
return env.equals("prod") ? new ProdClient() : new MockClient();
}
}Tie to config-driven wiring (Spring @Bean is factory at scale).