Design by Structural Principles¶
Chapter Info
Calculating...
!!Good software design needs a structure that makes change easier to manage. Separation of Concerns, Single Responsibility, Open-Closed, and Dependency Inversion provide four complementary ways to organize a system.!! Together, they help define clear boundaries, assign responsibilities, support extension, and control dependencies.
Introduction¶
Structure Is About Containing Change¶
!!A system can work perfectly today and still be poorly designed for tomorrow. The real test comes when something changes.!! In a poorly structured system, even a small change can have unexpected consequences across unrelated parts. In a well-structured system, changes remain easier to understand, manage, and contain. !!Good structure limits the impact of change.!!
Separate What Changes for Different Reasons¶
!!Not everything in a system changes for the same reason or at the same pace.!! When unrelated concerns are mixed together, changing one can unexpectedly affect the others. Good design recognizes these differences and keeps them from becoming unnecessarily entangled. !!Things that change for different reasons should be able to evolve independently.!!
Give Every Responsibility a Clear Owner¶
!!A system becomes difficult to maintain when responsibilities are unclear or scattered across multiple components.!! When each responsibility has a clear place, it becomes easier to understand where changes belong and what they might affect. !!Clear responsibilities make the system easier to understand and evolve.!!
Extend Without Breaking What Works¶
!!Software must evolve as new needs emerge.!! But adding functionality should not repeatedly put existing behavior at risk. !!Good design anticipates that systems will grow and makes room for new capabilities without unnecessarily disturbing what already works.!! Growth should not come at the expense of stability.
Depend on What Matters, Not How It Is Done¶
Not everything in a system has the same importance or the same lifespan. !!Some decisions define what the system must accomplish, while others concern how it happens to work today. Good design recognizes this difference and prevents temporary choices from controlling essential behavior.!! What matters most should not be dictated by what may change tomorrow.
Keep Different Concerns Separate¶
Separation of Concerns (SoC)¶
!!Separation of Concerns (SoC) organizes a system so different concerns are handled by distinct components.!! Business rules, presentation, storage, and integration serve different purposes and evolve for different reasons. Keeping them separate makes the system easier to understand, change, and test, while reducing the risk that a change in one concern affects unrelated parts.
SoC at Every Scale¶
!!Separation of Concerns applies at every scale, from functions and classes to modules and services. At each level, the goal is the same: keep related behavior together and separate concerns that evolve independently.!! The right boundaries depend on the context, but the question remains: does each part have a clear and coherent purpose?
Cohesion: The Other Half¶
!!Separating is only useful if what stays together belongs together. Cohesion is the silent partner of SoC: each separated unit must itself be tightly focused around a single purpose.!! Without cohesion, you do not get clean modules — you get a pile of fragments, each as confused as the original system, just smaller. Good structure balances high cohesion within components with low coupling between them.
Boundaries Need Contracts¶
!!Separating concerns creates boundaries, but boundaries need clear contracts to work. A contract defines what each component provides and what others can expect from it.!! Without explicit contracts, components may still depend on hidden assumptions or shared implementation details. Good contracts allow components to evolve independently while preserving their collaboration.
Related chapter
Design for Testability shows how explicit contracts make isolated components and their integrations easier to verify.
Give Each Component One Responsibility¶
Clear Responsibilities Shape Architecture¶
!!In software design, a responsibility defines the purpose of a component: what it does, which decisions belong to it, and where its boundaries end.!! Clear responsibilities help developers understand where behavior belongs and which component should handle a given requirement. Good architecture makes these boundaries explicit, so every part of the system has a well-defined role.
From Separation to Responsibility¶
!!Separation of Concerns and Single Responsibility are complementary principles that address different aspects of software structure. SoC asks what should be separated. SRP asks who should own each responsibility.!! SoC identifies concerns that evolve for different reasons, while SRP ensures each component has a clear and focused responsibility. Together, they create boundaries that keep changes local and prevent unrelated responsibilities from becoming tangled.
One Reason to Change: Single Responsibility Principle (SRP)¶
!!In a well-designed system, each component should have one clear reason to change, representing one group of stakeholders. This is known as the Single Responsibility Principle (SRP). A responsibility is not simply an operation: it is a coherent reason for a component to evolve.!! A component can perform several related operations while still serving one responsibility.
When Responsibilities Become Tangled¶
!!When multiple stakeholders, such as HR, Accounting, and Database Administration, influence the same component, changes requested by one group can unintentionally affect the others. A module that handles payroll processing, employee records, and data storage mixes responsibilities that evolve for different reasons. Keeping these responsibilities separate reduces unintended dependencies and makes changes easier to understand and maintain.!! But separation should not become fragmentation: keep together what changes together.
Extend Without Changing What Already Works¶
Open-Closed Principle (OCP)¶
!!The Open-Closed Principle (OCP) encourages software structures that allow new behavior to be added without repeatedly modifying stable, existing code. Components should be open for extension while remaining closed to unnecessary modification.!! The goal is not to eliminate changes, but to make extensions safer and reduce the risk of breaking what already works.
OCP in Code¶
In a reporting system, adding a new export format should mean creating a new exporter, not adding another if format_type == ... condition to the core.
Interfaces Define Extension Points¶
!!An interface defines a contract that multiple implementations can follow.!! For example, CSV, PDF, and XML exporters all implement the same Exporter interface. !!The core system works with this contract without knowing which implementation is used.!! New formats can be added without changing the existing logic.
Inheritance and Composition Enable Extension¶
!!Inheritance allows a class to extend or specialize the behavior of another class. Composition builds functionality by combining independent components. Both can support OCP by allowing new behavior without rewriting existing logic.!! Composition is often more flexible because components can be replaced or combined without changing class hierarchies.
Plugins: Extending Without Changing the Core¶
!!At the system level, the Open-Closed Principle can be applied through plugin architectures. Plugins add new capabilities through predefined extension points, allowing a system to grow without repeatedly modifying its core.!! The framework provides the mechanisms for integrating extensions, while each plugin implements its own behavior. New functionality can be introduced without making the core responsible for every variation.
Self-Describing Extensions¶
!!As systems grow through plugins or adapters, each extension should describe its own capabilities and requirements rather than forcing the framework to handle every special case.!! The framework provides common mechanisms for discovery, validation, and policy enforcement, while each extension defines its specific behavior and needs. !!This allows new extensions to be added without repeatedly changing the core.!!
Keep the Framework Generic¶
!!The framework should offer generic mechanisms—discovery, schema validation, policy enforcement—not a growing list of special cases.!! A thin registry for wiring is fine; embedding extension-specific logic in the center is not. This extends the Open-Closed idea: new behavior ships mostly in the new module, not in an omniscient orchestrator.
Extend for Real Needs, Not Imagined Ones¶
!!Extension points should address real or foreseeable changes, not every possibility.!! Adding interfaces and abstractions for hypothetical requirements creates unnecessary complexity. !!Start with a simple design and introduce extension points when variation becomes visible.!! OCP is about making change easier, not predicting every future change.
Keep the Core Independent (Dependency Inversion Principle)¶
!!A well-structured system separates what defines its behavior from the technologies used to deliver it.!! Business rules should remain independent of databases, frameworks, and external services, so technical choices can evolve without reshaping the core. !!The Dependency Inversion Principle provides a way to establish and preserve that boundary.!!
Decoupling Implementation Details¶
!!Business logic should express what the system needs to accomplish, not the technical steps required to achieve it.!! Business operations such as deciding whether an order can be fulfilled or when a payment is required belong at the business level, while their technical implementations remain behind clear boundaries.
Identifying Core and Details¶
To keep the core independent, we first need to distinguish the core from the details. !!The core defines what the system does: its business rules and essential behavior. A detail is something we can replace without changing those rules — a database, a framework, or an external service.!!
Let the Core Drive the Details¶
!!The core defines the purpose and behavior of the system. Technical decisions should support those needs, not dictate them.!! Databases, frameworks, and cloud providers may change, but the essential business rules should remain independent of those choices. The core sets the direction; the details must follow.
Let the Core Set the Terms for Its Dependencies¶
The core should define what it needs from the outside world, without depending on specific technologies. It establishes contracts that express its requirements, while databases, frameworks, and external services provide implementations that satisfy them.
Why Is It Called Dependency Inversion?¶
!!This way of structuring dependencies is called the Dependency Inversion Principle (DIP).!! The name comes from reversing a common dependency in code. !!Normally, business logic calls a database and depends on its implementation. With DIP, the business logic defines the contract it needs, and the database implementation depends on that contract.!! The execution flow still reaches the database, but the source-code dependency now points toward the contract defined by the core.
Define Contracts Around Business Needs¶
!!A good contract describes what the business needs, not how a particular technology works.!! For example, an order service may need to send a notification, without knowing whether it will be delivered by email, SMS, or another provider. The contract expresses that need, while each implementation decides how to fulfill it. !!Introduce abstractions where they protect the core from details that may change, not for every dependency.!!
Dependency Injection: Connecting Contracts to Implementations¶
!!Once the core depends on contracts rather than concrete implementations, something must connect them. This is the role of Dependency Injection (DI): the required implementations are provided to the business logic from outside, rather than created inside it.!! A composition root assembles these components, manually or through a DI framework. !!Dependency Injection connects the pieces; Dependency Inversion defines how they should depend on each other.!!
Keep Implementation Details Out of the Core¶
!!Implementation details should stay outside the business core.!! When domain logic contains SQL queries, framework-specific annotations, or vendor SDK calls, technical choices become embedded in business rules. This makes the core harder to change, test, and maintain. Keep these details behind clear boundaries so the business logic remains independent of how they are implemented.
Independence Makes Testing Easier¶
!!When business logic depends on contracts rather than concrete implementations, tests can replace real dependencies with mocks or stubs.!! This allows business rules to be tested without databases, external services, or other infrastructure. !!The same independence that makes technical details replaceable also makes the core easier to test.!!
Related chapter
Design for Testability explores test seams, mocks, contracts, and the broader testing strategy that dependency inversion enables.































