Creational patterns
21 questions in 6 subtopics.
Singleton and its variants
Write a thread-safe, lazily initialised singleton in Java. Compare double-checked locking, the initialization-on-demand holder idiom, and an enum singleton.Explain what can go wrong with this code under concurrency:class Config {
private static Config instance;
static Config get() {
if (instance == null) {
synchronized (Config.class) {
if (instance == null) {
instance = new Config();
}
}
}
return instance;
}
}
Factory method
Explain the factory method idea and static factory methods in Java. Give JDK examples and the benefits over public constructors.Refactor this code so a new payment provider can be added without editing it:PaymentClient create(String type) {
switch (type) {
case "STRIPE": return new StripeClient(cfg);
case "ADYEN": return new AdyenClient(cfg);
default: throw new IllegalArgumentException(type);
}
}
ServiceLoader) underpin pluggability in the JDK. When designing an internal platform meant to last ten years, how do you decide what to make pluggable through factories and what to keep fixed?Abstract factory
DocumentBuilderFactory.newInstance() returns a factory, and the factory returns a DocumentBuilder. Which pattern is this, and why is it designed this way?When is the abstract factory pattern justified? Give an example from Java or from cloud integrations, and describe its costs.Leadership wants every service to use an internal abstract factory that hides AWS and GCP clients behind common interfaces, so the company can switch clouds later. Evaluate the proposal.Builder
Write a builder for an immutableHttpRequestSpec class with a required URL and optional headers and timeout. Validation should happen when build() is called.A configuration object has 15 fields, four required and the rest optional with defaults. Compare telescoping constructors, a builder, a record with a compact constructor, and with-style copy methods. Which do you choose?Set the standard for constructing objects in shared libraries and services across 25 teams: hand-written builders, Lombok @Builder, or records with compact constructors. What do you decide and why?Prototype
What problems doesObject.clone() have, and what are better ways to implement the prototype idea of copying an existing object in Java?A pricing engine loads expensive rule templates from files at startup and creates a modified copy for each of 2,000 tenants with small overrides. Memory use is high and some tenants see each other's changes. How would you redesign this?Java serialization, which creates objects without calling their constructors, has caused many serious security vulnerabilities. What is the long-term lesson about creating objects outside constructors (clone, deserialization, reflection), and what standards follow for a large organisation?Dependency injection as a pattern
Explain constructor injection compared with field injection, and why constructor injection is the recommended default in Spring.Without any DI framework, show how you would wire a small Java application of a controller, a service, a repository and a clock using dependency injection. What is a composition root?A large Spring codebase owned by 20 teams mixes constructor injection, staticApplicationContext lookups, and hand-written singletons. How do you standardise it without stopping feature work?Spring has dominated Java dependency injection for two decades, while frameworks such as Micronaut, Quarkus and Dagger do more of the injection work at build time. How do you frame the long-term bet on DI for your platform?No question matches these choices.