Designing Classes Around Reasons to Change

The Single Responsibility Principle says that a class or module should have one cohesive responsibility: one reason to change. You use it when deciding which behaviour belongs together. A class can perform several operations while serving one responsibility. Conversely, a short class can combine unrelated policies. The useful question is whose decisions would require you to revise its behaviour.

Find the source of change

An actor is a stakeholder role or cohesive group whose policy decisions create a reason for software to change. Here, actor does not simply mean anyone who uses the application. Finance staff, for example, may own billing rules even though customers also interact with invoices.


The governing idea is:


one responsibility⟷one cohesive source of change\text{one responsibility} \longleftrightarrow \text{one cohesive source of change}


This is a design criterion, not a numerical test. Several people may represent the same policy interest, and one department may contain independently changing policy areas. Use actual authority and likely changes to interpret the boundary.


Ask what a proposed change would alter. A new discount rule changes billing policy. A revised document layout changes presentation policy. A different database schema changes persistence details. These changes can concern the same invoice without belonging to the same responsibility.



Choose a cohesive boundary

Suppose an invoice class calculates charges, arranges the customer document, and builds database commands. Finance, communications, and the data platform team independently control these decisions. Combining their policies means a request from any one of them changes the same class.


A useful design assigns billing rules to InvoiceRules, document presentation to InvoiceDocument, and storage details to InvoiceStore. These classes may cooperate and exchange invoice data. The principle concerns which decisions each class owns; it does not require classes to work in isolation.


Three policy owners connect to classes responsible for billing rules, document presentation, and database storage.


Example: A billing class might calculate a subtotal, apply an approved discount, and calculate the resulting charge. If these operations jointly implement one coherent billing policy, separate method names do not establish separate responsibilities.


Observation

What it tells you

Several methods implement one billing policy

They may belong in one class.

One method mixes billing rules and document layout

A single method can contain independent responsibilities.

Two classes use the same invoice record

Shared data alone does not decide their responsibilities.

Two policy areas currently change together

Timing alone does not establish common ownership.


Count independent reasons to change, rather than methods. Splitting every operation into a class can scatter a single policy across many places without improving its boundary.

Evaluate a proposed design

Describe each class's responsibility in terms of the policy it implements. Then test that description against plausible requests. Would a new discount rule require editing presentation decisions? Would a layout revision require touching database commands? Such edits are evidence that independent responsibilities may still be mixed.


A name alone is insufficient. A class called InvoiceService can own unrelated policies, while a larger class with a precise policy boundary can satisfy the principle. Likewise, moving every policy into one shared helper preserves the original mixture under a different name.



Keep conclusions proportional to the evidence. Clear ownership and representative change requests support a design decision; class counts and file lengths do not prove it. A smaller change footprint can be a benefit, but it is not the definition.

What to remember

  • Identify the stakeholder role and policy behind a reason to change.
  • Keep operations together when they serve one cohesive responsibility.
  • Separate independently changing policies even when they share data.
  • Judge the decisions a class owns, rather than its name, length, or method count.

Example: Responsibilities in a ticketing service

A theatre's TicketOffice class calculates ticket charges, formats customer receipts, and builds database commands. Finance independently controls charging rules, communications controls receipt appearance, and the data platform team controls database details. A change owner is the role authorized to decide the policy being changed.


For this example, the team's agreed checks take 7 min for charging rules, 11 min for receipt presentation, and 6 min for database storage. With the combined class, its testing policy requires all three checks after every request. With separate classes, assume each request changes only its designated class and requires only that responsibility's check. These are explicit workload assumptions, not general guarantees about testing.


The next batch contains two independent charging requests, one receipt-layout request, and one database request. Each request is implemented and checked separately; setup time is ignored.


a) Identify the independent reasons for the current class to change.


Finance can revise how charges are calculated. Communications can revise how receipts look. The data platform team can revise how ticket records are stored. Each role can make its decision independently of the others.


The current class therefore combines three responsibilities: charging policy, receipt presentation, and database storage. Their use of the same ticket data does not make their policy ownership identical.


b) Propose class boundaries that follow the Single Responsibility Principle.


Assign charging decisions to TicketCharges, receipt appearance to TicketReceipt, and database commands to TicketStore. Each class then owns one of the independent policies identified in part a).


The classes may still exchange ticket data. TicketCharges may contain several calculations, and TicketReceipt may contain several formatting operations. The proposal groups operations by their reason to change rather than creating a class for each method.


The proposed boundaries are charging rules, receipt presentation, and database storage. Actual implementations would still need inspection to confirm that policy decisions remain within those boundaries.


c) Calculate checking time for the batch under each design and the time saved under the stated assumptions.


First calculate the complete checking time for one request affecting the combined class. The sum of the three checks is ________.


tsuite=7+11+6=24 mint_{\text{suite}}=7+11+6=24\ \text{min}


There are four separately checked requests. The combined class therefore requires:


Tcombined=4(24)=96 minT_{\text{combined}}=4(24)=96\ \text{min}


For the separated design, the two charging requests contribute ________, while the receipt and database requests together contribute ________.


Tseparated=2(7)+1(11)+1(6)=14+11+6=31 min\begin{aligned} T_{\text{separated}}&=2(7)+1(11)+1(6)\\ &=14+11+6\\ &=31\ \text{min} \end{aligned}


Subtract the separated workload from the combined workload:


ΔT=96−31=65 min\Delta T=96-31=65\ \text{min}


The batch requires 96 min with the combined class and 31 min with the separated classes, a saving of 65 min. The saving depends on the supplied checking policy and change assumptions. It illustrates a possible benefit of separating responsibilities; it does not establish a universal testing reduction or define the principle.

A university's FeeCalculator has methods for tuition charges, approved fee reductions, and the final payable amount. All implement one coherent fee policy owned by the finance office. Which assessment best applies the Single Responsibility Principle?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A library renewal service separates approval rules, membership charges, notice presentation and the sequence connecting them. Match each class to its focused obligation. The coordinating class passes returned values between components without implementing their approval, charging or presentation rules.

Match each term
A.

Produces the wording and layout of the message using the supplied approval decision and amount.

B.

Applies the library's rules about whether outstanding returns prevent a member from renewing.

C.

Calculates the renewal amount using the approved rates for the member's category.

D.

Requests an approval decision, obtains the applicable charge and passes the returned values to the notice component.

RenewalNotice

RenewalCoordinator

MembershipCharge

BorrowingEligibility

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A training provider assigns admission requirements to its academic office, customer-document appearance to communications, and database details to its data team. Use the words eligibility, presentation, and storage to complete the responsibility analysis.


A request to change the typeface on admission letters concerns . A request to require an additional prerequisite course concerns . These requests have reasons to change; choose independent or identical for the final blank.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice