Structural patterns
20 questions in 7 subtopics.
Adapter
Explain the adapter pattern and give examples from the JDK and Spring.You must integrate three shipping carriers with different APIs, data models and error codes behind oneShippingGateway interface used by the order domain. Design the adapters and explain how you test them.You must migrate 150 services from one HTTP client library to another over a year. How can the adapter pattern help, and how do you avoid leaving a permanent extra layer?Decorator
Write a decorator that adds retry around this interface:interface PriceService {
Price price(String sku);
}
new MetricsClient(new RetryClient(new CachingClient(realClient))). Dashboards show fewer failures than the remote service reports, and cache hits are counted as very fast remote calls. Explain what the order does and propose a better order.Over two decades Java moved from hand-written proxies and decorators to frameworks generating them at run time (Spring AOP, bytecode generation), and now toward build-time generation for AOT and native images. What does this mean for how you design cross-cutting concerns in systems expected to live ten or more years?Proxy
Spring AOP uses proxies for@Transactional. Explain JDK dynamic proxies versus class-based (CGLIB-style) proxies, and why a @Transactional method called from another method in the same class runs without a transaction.Write a JDK dynamic proxy that logs the duration of every call to any interface, and describe the pitfalls.Remote proxies, such as RMI stubs, EJB remote interfaces and declarative HTTP clients, make a network call look like a local method call. What problems has that caused, and what guidelines would you give teams that use declarative HTTP clients today?Service meshes moved retries, mutual TLS and routing from in-process libraries into sidecar proxies, and newer designs move some of it into node-level or proxyless modes. How should you decide where cross-cutting network concerns live over a decade?Facade
What is the facade pattern, and how does it differ from adapter? Give Java examples.AnOrderFacade class has grown to 3,000 lines and 80 public methods used by many controllers and jobs. Every change causes merge conflicts. What do you do?A platform team proposes a company-wide facade library that wraps Kafka, HTTP clients and database access for all services. Evaluate it and describe how you would design it if approved.Composite
Model pricing eligibility rules where a rule can be a single condition (such as minimum order total) or a group combining other rules with AND or OR. Write the interface and classes using the composite pattern.Compare a classic polymorphic composite (each node implements evaluate) with a sealed interface of records plus switch pattern matching for an expression tree in Java 21 and later. When do you choose each?Bridge
A notifications module has classesOrderEmail, OrderSms, OrderPush, AlertEmail, AlertSms, AlertPush, ReminderEmail and so on. Refactor it with the bridge pattern.Explain the bridge pattern and show how JDBC or SLF4J fits it. How does bridge differ from adapter?Flyweight
Explain the flyweight pattern and give JDK examples. What property must flyweight objects have?A market data service keeps 30 million Trade objects in memory. A heap histogram shows that String objects for the exchange code and currency, with only a few hundred distinct values, use several GB. How would you reduce this?Memory-saving techniques such as flyweights and interning matter at scale. Over years, how do you decide whether to remove duplication in application code or rely on runtime features such as compact strings, string deduplication and possible future value classes?No question matches these choices.