Adapter, decorator and facade - what concrete problem does each one solve, and where would reaching for it be over-engineering?
Answer
Short answer
Adapter converts a type you do not own into the interface your code already speaks. Decorator adds behaviour while keeping the same interface, so wrappers stack. Facade gives callers one narrow entry point into a subsystem.
Keep structural patterns explicit in the answer because that is the concept the interviewer is actually trying to test. A good structural patterns explanation names the trade-off, the failure mode, and the evidence you would use before choosing. Use structural patterns once more at the decision point so the answer reads as judgement rather than a detached example.
Keep structural patterns explicit in the answer because that is the concept the interviewer is actually trying to test. A good structural patterns explanation names the trade-off, the failure mode, and the evidence you would use before choosing.
Was this answer helpful?
Likely follow-ups
- How do you keep a stack of decorators debuggable when a stack trace is ten frames of wrappers?
- What breaks when a decorator wraps an object whose identity or equals contract callers rely on?
- Where would you put a retry - a decorator, the adapter, or the caller?
- If a facade grows to forty methods, what has gone wrong and what do you do?
Related questions
- When would you use the observer pattern, and what goes wrong with it at scale?mediumAlso on coupling4 min
- Give me a real single-responsibility violation you have seen, and how would you refactor it?mediumAlso on coupling5 min
- Service A needs something from service B. When should that be a synchronous call and when should it be an event?mediumAlso on coupling4 min
- Two teams need the same customer data. Do you give them one shared database, or two copies that are allowed to disagree?hardAlso on coupling6 min