Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single Responsibility Principle in OOP

In my application design, I usually map objects to the important tables in the database. The objects then handle everything relating to that data (including linkage tables). So I for example have built an Activity object, with properties like name and due_date, methods like load() and save(), and also methods like getParent(), getContributors() and getTeam(), which return (arrays of) other objects. Is this 'bad' OOP because it violates the Single Responsibility Principle?

like image 576
Rijk Avatar asked Sep 14 '11 08:09

Rijk


1 Answers

It depends on the situation and the exact code you have: Your design might touch multiple responsibilities and still be a pretty nice OOP and maintainable.

Do you handle load() and save() in each class with similar code? Or do you delegate the task within load() and save() to other objects that are used for this functionality in several classes? That would be half-what following SRP and still be according to your design.

If not, your code really seems a bit smelly. To check whether it covers multiple responsibilities, ask yourself: what could cause changes to my class? In your situation, I would at least try to refactor the similar code in load() and save() in different classes to reach the situation described above, so that

  • maintainability is greatly improved,
  • you still do not need to change your clients' code.
like image 96
DaveFar Avatar answered Oct 19 '22 04:10

DaveFar