Skip to main content

Single Responsibility Principle (Software Design)

The Single Responsibility Principle (SRP) is a Software Design Principle that states that there should never be more than one reasons to change a particular class.

"A class should only have one reason to change." [📖ASD, p. 95]

The Single Responsibility Principle is one of the SOLID-Principles.

A design that violates this principle would be a class that contains business logic as well as logic for persisting this logic, such as an Accounting-class: The Accounting class is responsible for managing salaries and payments in a company. In the current design, the class also contains logic for establishing connections to the database:

Figure 1 If the infrastructure changes, Accounting has to change.

The responsibility forAccounting should be clearly to orchestrate payment and salary information, but with the infrastructure hardwired into the class, Accounting now has to change whenever the infrastructure changes.

The following design resolves this violation: The concern of persisting the data is clearly decoupled from the concern to process the business logic given the context of Accounting.

Figure 2 If the infrastructure changes, Accounting has no need to change.

see also