Single Responsibility Principle

noun

say it: “SING-gul rih-SPON-suh-BIL-ih-tee PRIN-suh-pul” /ˈsɪŋɡəl rɪˌspɒnsəˈbɪlɪti ˈprɪnsəpəl/

The design rule that each function, class or module should have one job, so it has only one reason to change.

What is Single Responsibility Principle?

The Single Responsibility Principle says a class should have one job, so it has only one reason to change. A class that calculates payroll should not also format the PDF report.

// Split one big class into focused ones
public class PayrollCalculator { /* works out pay */ }
public class PayslipPdfWriter  { /* turns pay into a PDF */ }

In a sentence

  • “Following the single responsibility principle, I split one long function into calculateTotal and printReceipt.”

Level: Freelancer · More OOP terms at this level

Updated