Course contentsShow
← Software Engineering
Lesson 195 of 1,9336. SOLID PrinciplesFree lesson

Identifying Multiple Responsibilities

Recognize when a class violates SRP by handling unrelated concerns like business logic, persistence, and formatting.

Identifying Multiple Responsibilities

You'll learn: How to spot when a single class is doing too many unrelated jobs, which violates the Single Responsibility Principle.

The Warning Signs

A class violates SRP when it handles unrelated concerns—different types of work that don't naturally belong together. The most common culprits are mixing:

  • Business logic (the rules and decisions your application makes)
  • Persistence (saving and loading data from databases or files)
  • Formatting (converting data for display, reports, or APIs)

Think of a restaurant employee who simultaneously cooks meals, manages inventory, and greets customers. Each task requires different skills and changes for different reasons. When the greeting script changes, you shouldn't need to touch cooking procedures.

How to Spot Multiple Responsibilities

Ask yourself: "Why would this class need to change?"

If you can list multiple, unrelated reasons, you've found multiple responsibilities:

  • "We need to change how we calculate discounts" (business logic)
  • "We're switching from MySQL to PostgreSQL" (persistence)
  • "Marketing wants invoices formatted differently" (formatting)

Each reason represents a different responsibility—a different "axis of change." A well-designed class should have only one reason to change.

Real-World Example (Conceptual)

Imagine an Invoice class that:

  1. Calculates totals and applies tax rules
  2. Saves itself to a database
  3. Generates PDF reports

This class has three distinct responsibilities. When tax laws change, database schemas evolve, or PDF formatting requirements shift, this single class must be modified—increasing the risk of breaking unrelated features.

Key Takeaway: A class has multiple responsibilities when it would need to change for multiple, unrelated reasons—like mixing business rules with data storage or output formatting.