Behavioral patterns
29 questions in 10 subtopics.
Strategy
This method grows every time marketing adds a customer type:BigDecimal discount(Customer c, BigDecimal total) {
if (c.type() == GOLD) return total.multiply(new BigDecimal("0.10"));
else if (c.type() == SILVER) ...
}
Observer
Explain the observer pattern, give Java and Spring examples, and say whyjava.util.Observable was deprecated.An @EventListener for OrderPlacedEvent sends a confirmation email. Sometimes customers receive the email although the order transaction rolled back. Explain and fix.Many modules in your services react to in-process Spring application events. A team proposes replacing all of them with Kafka topics for consistency. How do you decide?Template method
Explain the template method pattern and give examples from the JDK and the Servlet API. How does Spring'sJdbcTemplate relate to it?An AbstractImporter base class has 12 subclasses, each overriding between two and five hook methods. Changes to the base class often break subclasses. How would you refactor it?Command
Explain the command pattern with Java examples, and describe how it supports undo.Design a background job system where commands are stored in a database and executed by workers, surviving restarts and retried on failure.Your company implements long-running business processes with home-grown command queues and state machines. Should it adopt a durable workflow engine across teams? Frame the multi-year decision.Chain of responsibility
Servlet filters and the Spring Security filter chain follow the chain of responsibility pattern. How does each filter decide to continue or stop, and why does order matter?A custom authentication filter declared as a Spring@Component and also added to the SecurityFilterChain with addFilterBefore runs twice per request. Why, and how do you fix it?Design a shared Spring Boot starter that gives 200 services a standard request filter chain: authentication, tenant resolution, rate limiting and request logging. How do you handle order, extension and rollout?State
Implement an order lifecycle with states NEW, PAID, SHIPPED and CANCELLED, where only valid transitions are allowed (for example SHIPPED cannot become CANCELLED). Show a Java approach.A loan application workflow has 15 states, several approval steps, and regulators require an audit of every transition. Compare the state pattern, an explicit transition table, a state machine library, and a workflow engine.Six services each implement their own version of the order lifecycle, and orders end up in inconsistent states across systems. Lead the alignment.Visitor
Where does the JDK use the visitor pattern? Explain howFiles.walkFileTree with a FileVisitor works.Compare the visitor pattern with sealed interfaces and switch pattern matching in Java 21 for adding operations to an AST of expression nodes. When is visitor still the better choice?Lambdas, records, sealed types and pattern matching have absorbed several GoF behavioral patterns: strategy into lambdas, visitor into exhaustive switches, command into records and Runnable. How should a long-lived codebase with years of pattern-heavy code evolve, and what is the risk of mass rewrites?Iterator
This code throwsConcurrentModificationException:for (Order o : orders) {
if (o.isCancelled()) orders.remove(o);
}
Iterator<Customer> over a REST API that returns customers in pages of 100 with a next-page token. Pages must be fetched lazily. How would you also expose it as a Stream?Mediator
Explain the mediator pattern, and describe howDispatcherServlet in Spring MVC acts as a mediator.A team introduces an in-process command bus: controllers send command objects, and the bus finds a handler by type. After a year, engineers say they cannot follow the code. Evaluate the mediator approach and suggest improvements.Enterprise service buses acted as company-wide mediators that routed, transformed and orchestrated messages, and many organisations later moved away from them. What lessons apply to central orchestration components such as API gateways and workflow engines today?Memento
Implement undo for a text editor with the memento pattern in Java, and explain why the memento should not expose the editor's internals.A form-heavy application must support undo across many edits on large objects. Compare full-state mementos with storing commands or diffs.A workflow service saves snapshots of process state so it can roll back after failures, and the snapshot table has grown to billions of rows. How do you rethink this?No question matches these choices.