Design Principles
Design Principles: Reasoning About Change
Design principles help you judge whether software can accommodate a change without spreading edits or breaking existing behavior. They guide decisions rather than mechanically choosing a design for you. At this level, the useful question is not simply whether a design contains interfaces or small classes. It is whether those choices protect the behavior and dependencies that matter for a plausible change.
Principles have a shared purpose
The SOLID principles address related risks from different directions. Single responsibility concerns independent reasons for change. The other principles help you reason about extension, replacement and dependencies across module boundaries. These ideas reinforce one another, but satisfying one does not establish that the others hold.
The open–closed principle asks you to design a module so that relevant new behavior can be added through extension while its stable implementation remains unchanged. For example, a report generator might accept interchangeable output writers. Adding a writer can then accommodate a new format without adding another format-specific branch inside the generator.
This protection is relative to a kind of change. A writer interface may support new output formats while offering no protection against a completely new reporting workflow. Always name the change a design is intended to accommodate before claiming that it supports extension.
How the principles work together
Principle | Question to ask | Relationship to the others |
|---|---|---|
Single responsibility | Do independent sources of requirements drive changes to this module? | Separating reasons for change can make useful extension boundaries easier to identify. |
Open–closed | Can the intended variation be added without editing the stable module? | Extension relies on suitable interfaces and compatible replacements. |
Liskov substitution | Can a replacement preserve the behavior promised to existing clients? | An extension that breaks client expectations is not a safe replacement. |
Interface segregation | Does a client depend on operations it does not need? | Focused interfaces can reduce unnecessary dependencies without changing implementation behavior. |
Dependency inversion | Do policy and implementation details depend on suitable abstractions? | This dependency direction can let implementation details vary behind a stable boundary. |
The dependency inversion principle directs high-level policy and low-level implementation details toward abstractions rather than making policy depend directly on those details. The abstractions should express the policy's needs; they should not simply copy every operation offered by a vendor library.
A narrow interface can support both dependency inversion and interface segregation. However, a replacement implementation still has to preserve the interface's promised behavior. Matching method names and parameter types does not establish compatible results, accepted inputs or failure behavior. Structural compatibility and behavioral compatibility answer different questions.
Evaluate benefits against costs
Use a concrete change scenario to compare designs. Count the affected modules, distinguish edits from rebuilding, and include the work needed to add new implementations. Under a stated additive effort model, total change effort is
where ti is the estimated work on affected module i, measured in minutes. This is a comparison model, not a universal law of software maintenance.
Example: Suppose adding an output format requires three existing modules to be edited, at 20 minutes each. The estimate is 60 minutes. An alternative requires a 15-minute configuration edit and a new writer taking 25 minutes, for 40 minutes in total. Under those assumptions, the extension point reduces estimated work by 20 minutes. The calculation does not establish that every future change will be cheaper.
Abstractions also require design, documentation and maintenance. Prefer a boundary that protects a credible variation over several layers introduced only to satisfy a checklist. Revisit the decision when the expected changes become clearer.
What to remember
- Judge a design against a stated change and existing client expectations.
- Extension, focused interfaces and dependency direction can reinforce one another.
- Replacement implementations must preserve promised behavior.
- Separate source edits, rebuilding and estimated effort when comparing designs.
Example: Adding an Invoice Export Format
An invoicing application must add a new export format. In the current design, invoice-processing code directly calls format-specific libraries. The proposed design makes that code depend on an ExportWriter interface expressing its output needs. Concrete writers implement the interface and call their format libraries; configuration supplies the chosen writer.
For this change, the current design requires four existing-module edits at 30 minutes each. The proposed design requires one configuration edit taking 15 minutes and a new writer taking 45 minutes. Assume these estimates include all implementation, integration and testing work, and that the times add without overlap.
The interface promises that exporting any valid invoice returns a nonempty retrieval identifier. Exporting an invoice with a zero total is explicitly valid.
a) Explain how the proposed dependency direction supports dependency inversion and how adding this format illustrates the open–closed principle.
The high-level invoice-processing code names ExportWriter rather than a format-specific library. Each concrete writer names that same interface and its own library. Policy and implementation details therefore depend on the abstraction, supporting dependency inversion.
Configuration can supply the new writer without changing the invoice-processing code. Adding this output format therefore extends the system while leaving that stable implementation unchanged. This supports the open–closed principle for format additions; it does not establish that every future invoicing change will avoid edits.
b) Calculate the percentage reduction in estimated effort relative to the current design.
Here, change effort means the total estimated work needed to complete the specified change. First calculate the baseline. The current design requires ________ existing-module edits at 30 minutes each.
For the proposed design, the configuration edit takes ________ and implementing the new writer takes ________. Include both contributions.
The saving is ________. Divide by the original effort because the comparison is relative to the current design.
The estimated effort falls by 50%. The proposed total is half the baseline, which checks the calculation. This result depends on the supplied estimates and does not measure every possible benefit of the design.
c) The new writer returns an empty string for zero-total invoices. It implements every required method. Is it a valid substitute under the stated interface promise? Explain.
A behavioral contract is the set of input requirements and observable promises on which a client may rely. Here, a zero-total invoice is a valid input, and a nonempty retrieval identifier is a promised result.
The new writer accepts the call but returns an empty string for that valid input. It therefore weakens the promised result and violates substitutability. Matching method signatures does not repair the behavioral failure.
The answer is no. The writer must return a nonempty retrieval identifier for zero-total invoices as well. Safe extension requires compatible behavior as well as a suitable dependency structure.
A ReceiptService class in a point-of-sale system holds both the tax-rate rules and the code that lays out printed receipts. The finance team asks for edits whenever tax rates change, and the store-design team asks for edits whenever the receipt layout changes. Under the Single Responsibility Principle, this class has more than one to change. Splitting it into TaxCalculator and ReceiptFormatter raises the of each class, because everything left inside each one serves a single purpose. The split also lowers the between tax logic and layout logic, so editing one no longer risks breaking the other.
A storage interface promises to accept every nonempty document up to 8 MiB and return a retrieval identifier. A new implementation has matching method signatures but rejects documents larger than 1 MiB. Clients already rely on the published limit. Assuming sufficient backend capacity, which change restores substitutability while preserving that interface contract?