Composition
Composition assigns an exclusive part to a whole. In a model, the whole controls the part’s lifecycle; deleting a reference in a garbage-collected language is not sufficient to express a business deletion.
What you’ll understand
- Composition assigns an exclusive part to a whole. In a model, the whole controls the part’s lifecycle; deleting a reference in a garbage-collected language is not sufficient to express a business deletion.
- Order cancellation explicitly marks its lines inactive and leaves the associated customer unchanged.
- Evaluate the tradeoff: Exclusive ownership simplifies invariants but makes sharing a part inappropriate. If a part must belong to several wholes, use references to an independent entity instead.
Useful first: Classes and Objects
The idea and when to recognize it
Composition assigns an exclusive part to a whole. In a model, the whole controls the part’s lifecycle; deleting a reference in a garbage-collected language is not sufficient to express a business deletion.
- Requirement: An order owns line items, while a customer has a separate identity outside the order.
- Change to anticipate: Cancel an order and invalidate its owned lines without deactivating the customer.
- Design goal: Order cancellation explicitly marks its lines inactive and leaves the associated customer unchanged.
Start with the concrete problem
An order owns line items, while a customer has a separate identity outside the order.
Cancel an order and invalidate its owned lines without deactivating the customer.
| Direct implementation | Revised design |
|---|---|
| Treating owned lines as independent records lets canceled orders retain active fulfillment work. | Order cancellation explicitly marks its lines inactive and leaves the associated customer unchanged. |
| The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design. | Exclusive ownership simplifies invariants but makes sharing a part inappropriate. If a part must belong to several wholes, use references to an independent entity instead. |
Responsibilities and contracts
Order cancellation explicitly marks its lines inactive and leaves the associated customer unchanged.
- Order: Controls line-item lifecycle. Public operations: cancel()
- LineItem: Belongs to one order. Public operations: active
- Customer: Has an independent lifetime. Public operations: active
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 document owns paragraphs while referencing an author whose account survives document deletion.
- 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
Exclusive ownership simplifies invariants but makes sharing a part inappropriate. If a part must belong to several wholes, use references to an independent entity instead.
Common mistake: Treating owned lines as independent records lets canceled orders retain active fulfillment work.
- Explain the choice: Order cancellation explicitly marks its lines inactive and leaves the associated customer unchanged.
- Challenge your choice: Exclusive ownership simplifies invariants but makes sharing a part inappropriate. If a part must belong to several wholes, use references to an independent entity instead.
- Describe a requirement that would make the simpler implementation sufficient.
Design challenge and knowledge check
Preparing the design walkthrough…