Scrivio Interview library

Creational patterns

21 questions in 6 subtopics.

Singleton and its variants

CodingFoundations7 min read
Write a thread-safe, lazily initialised singleton in Java. Compare double-checked locking, the initialization-on-demand holder idiom, and an enum singleton.
DebuggingFoundations6 min read
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;
}
}
ConceptIntermediate6 min read
How is a Spring singleton-scoped bean different from a GoF singleton, and in which situations does a classic static singleton still cause problems in a Spring application?
ScenarioAdvanced7 min read
A production incident shows one tenant's data in another tenant's response. The cause is a static singleton cache keyed only by product id in a multi-tenant service. Lead the fix and the standard that follows.
LeadershipPrincipal6 min read
The GoF creational patterns were described in 1994 for C++ and Smalltalk. Which of them remain relevant in modern Java with DI containers, records, lambdas and modules, and how would you teach this to senior engineers?

Factory method

ConceptFoundations6 min read
Explain the factory method idea and static factory methods in Java. Give JDK examples and the benefits over public constructors.
DesignIntermediate6 min read
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);
}
}
Explain your choice in a Spring application.
DesignPrincipal6 min read
Static factories and service providers (for example JDBC drivers and XML parser factories found through 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

ConceptFoundations6 min read
DocumentBuilderFactory.newInstance() returns a factory, and the factory returns a DocumentBuilder. Which pattern is this, and why is it designed this way?
ConceptIntermediate6 min read
When is the abstract factory pattern justified? Give an example from Java or from cloud integrations, and describe its costs.
ScenarioAdvanced7 min read
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

CodingFoundations7 min read
Write a builder for an immutable HttpRequestSpec class with a required URL and optional headers and timeout. Validation should happen when build() is called.
DesignIntermediate7 min read
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?
LeadershipAdvanced6 min read
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

ConceptFoundations6 min read
What problems does Object.clone() have, and what are better ways to implement the prototype idea of copying an existing object in Java?
ScenarioIntermediate7 min read
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?
LeadershipPrincipal6 min read
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

ConceptFoundations5 min read
Explain constructor injection compared with field injection, and why constructor injection is the recommended default in Spring.
DesignIntermediate6 min read
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?
LeadershipAdvanced6 min read
A large Spring codebase owned by 20 teams mixes constructor injection, static ApplicationContext lookups, and hand-written singletons. How do you standardise it without stopping feature work?
LeadershipPrincipal6 min read
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?