SRP - Single Responsibility Principle

So let's begin our quest for hard SOLID as a rock code!

No, not The Rock

SRP - Single Responsibility Principle has a simple definition:


A class should have only one reason to change.

If a class has more than one responsibility, they become coupled. But what is a responsibility? 



Always grumpy


A responsibility is just a reason to change.

Through here we extract a corollary: 


An axis of change is an axis of change only if the changes occur.

Separate everything just "because yes" would smell of needless complexity.


Let's see an example:

Mixed responsibilities

Employee class has two responsibilities: business rules and persistence control. Business rules tend to change frequently, and although persistence may not change as frequently, it changes for completely different reasons.

Responsibilities separated using DAO Pattern
Class Employee becomes decoupled of persistence, using DAO Pattern. Fortunately the practice of TDD will usually force these two to be separated.

So remember, always separate class responsibilities... when needed... maybe?




P.S: From the man himself






No comments:

Post a Comment