Separation of Concerns
Separate decisions with different responsibilities so one can change without disturbing the others. In a small program, functions may be enough; separation does not require a service for every operation.
What you’ll understand
- Separate decisions with different responsibilities so one can change without disturbing the others. In a small program, functions may be enough; separation does not require a service for every operation.
- Calculate a numeric subtotal first, then pass it to a dedicated renderer selected at the boundary.
- Evaluate the tradeoff: Separation introduces boundaries and data passed between them. Keep closely related work together when splitting it would only add indirection.
Useful first: Classes and Objects
The idea and when to recognize it
Separate decisions with different responsibilities so one can change without disturbing the others. In a small program, functions may be enough; separation does not require a service for every operation.
- Requirement: A report calculates a subtotal and then renders it as text or JSON.
- Change to anticipate: Switch output formats without duplicating calculation or turning the numeric total into presentation text early.
- Design goal: Calculate a numeric subtotal first, then pass it to a dedicated renderer selected at the boundary.
Start with the concrete problem
A report calculates a subtotal and then renders it as text or JSON.
Switch output formats without duplicating calculation or turning the numeric total into presentation text early.
| Direct implementation | Revised design |
|---|---|
| Combining arithmetic with formatting makes a second output format require a duplicate calculator. | Calculate a numeric subtotal first, then pass it to a dedicated renderer selected at the boundary. |
| The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design. | Separation introduces boundaries and data passed between them. Keep closely related work together when splitting it would only add indirection. |
Responsibilities and contracts
Calculate a numeric subtotal first, then pass it to a dedicated renderer selected at the boundary.
- Calculator: Produces numeric results. Public operations: sum(values)
- Renderer: Chooses output representation. Public operations: render(total)
Read the design
Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.
Preparing the design walkthrough…
Follow the interaction
Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.
Preparing the design walkthrough…
Implementation and expected output
Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.
Preparing the design walkthrough…
Try the idea in another setting
A form can separate field validation from persistence and from the message shown to the user.
- Name the new client contract before adapting the example.
- Identify which state belongs to each object and who controls its lifetime.
- Write one normal case and one boundary case before extending the design.
Tradeoffs and interview takeaways
Separation introduces boundaries and data passed between them. Keep closely related work together when splitting it would only add indirection.
Common mistake: Combining arithmetic with formatting makes a second output format require a duplicate calculator.
- Explain the choice: Calculate a numeric subtotal first, then pass it to a dedicated renderer selected at the boundary.
- Challenge your choice: Separation introduces boundaries and data passed between them. Keep closely related work together when splitting it would only add indirection.
- Describe a requirement that would make the simpler implementation sufficient.
Design challenge and knowledge check
Preparing the design walkthrough…