![]() |
| 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 |
So remember, always separate class responsibilities... when needed... maybe?
P.S: From the man himself





No comments:
Post a Comment