How does Builder differ from Chain of Responsibility?
Builder and Chain of Responsibility are two completely different patterns, even though both use a step-by-step or sequential approach. The difference is in the goal and the nature of the interaction between objects.
1. Goal
- Builder - controls the creation of a complex object from separate parts. It assembles the product step by step until a finished instance is produced.
- Chain of Responsibility - controls handling a request: it passes it along a chain of objects until one of them handles it.
2. Type of interaction
- In Builder, all steps run in a defined sequence, often under the control of a "director." Each step adds a part of the future object.
- In Chain of Responsibility, objects form a chain; each one can handle the request or pass it on. Here the interaction is dynamic and conditional, not a linear construction of a result.
3. Context of use
| Criterion | Builder | Chain of Responsibility |
|---|---|---|
| Goal | Create an object | Handle a request |
| Result | One finished object | Potentially different handlers |
| Control | A defined order of steps | Dynamic passing along the chain |
| Object relationship | Builder and director | Several handlers in a chain |
4. Example
Builder:
Assembles a Car: engine -> wheels -> interior -> build().
Chain of Responsibility: Handles a user request: logger -> authorization -> validation -> controller, until someone completes the chain.
5. Conclusion
- Builder is about construction (creating a product).
- Chain of Responsibility is about delegation (passing a request on).
Builder answers the question "How to build it step by step?", Chain of Responsibility answers "Who will handle it?".
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.