Java Behavioural patterns
How objects collaborate. Strategy and observer come up constantly because both are now one lambda long.
3 concepts · 9 interview questions
What this topic covers
Every concept in behavioural patterns, and the questions each one gets asked as. Where a question links, it has a full write-up.
Strategy
Selecting an algorithm at runtime by passing it in. Since Java 8 this is usually a lambda or a functional interface rather than a class hierarchy.
- How would you replace a long if-else chain over a type?
- Is a lambda a strategy?
- How do you pick a strategy by configuration?
Observer and events
Notifying interested parties without the source knowing who they are. Application events and message listeners are the same idea at different scales.
- What are the risks of the observer pattern?
- Synchronous or asynchronous listeners?
- How do Spring application events relate to this?
Template method, command and chain
Fixing the skeleton of an algorithm while letting steps vary, and turning a request into an object that can be queued, logged or undone.
- Where does Spring use the template method pattern?
- When is a command object worth the indirection?
- What is chain of responsibility, and where have you used one?