SOLID & OOP Principles (50 Qs)•Part 3: Enterprise Architecture, Package Design & Enforcement (25 - 35)
Single Responsibility Principle (SRP) and the "Reason to Change": Actor-driven responsibilities.
senior LevelSystem Design
In Simple Words: Robert C. Martin ("Uncle Bob") refined SRP: "A module should be responsible to one, and only one, actor (stakeholder/user group)":
- The Violation Example: An
Employeeclass containingcalculatePay()(owned by CFO/Finance),reportHours()(owned by COO/Operations), andsave()(owned by CTO/Database Admins). - The Consequence: When Finance requests a change to overtime calculation, the edit inadvertently breaks the Operations timesheet report, causing merge conflicts and production bugs.
- The Fix: Separate into independent classes (
PayCalculator,HourReporter,EmployeeRepository) operating over a shared immutable data structure.