1

Let's use as example:

class AccountDAO {
    create(){..}
    read(){..}
    update(){..}
    delete() {..}
}

How many responsibilities are there? 1 or 4?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ejaenv
  • 2,117
  • 1
  • 23
  • 28

1 Answers1

2

SRP shouldn't be understood in a strict manner. One object should have very few responsibilities, not "one".

Here AccountDAO is only responsible for Account persistence, so it has only one responsibility.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tristan
  • 8,733
  • 7
  • 48
  • 96
  • How should the responsability of this class be described? If I say "The AccountDAO is reponsible for Account persistance" this is 1 responsability, but if I say "The AccoundDAO is reponsible to create, read, update AND delete" these are 4. In general, the literature says that if you need to use ANDs to describe it, it means more than one responsability. So, which level of descripcion is the correct? – ejaenv Jul 12 '11 at 14:21
  • You'll have this problem of "granularity" (http://en.wikipedia.org/wiki/Granularity) your whole career if you work as a designer/programmer. For this specific DAO problem the "litterature" says it's better to implement all CRUD operations in one class (See http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html). Please mark my answer as valid if you think it is. – Tristan Jul 12 '11 at 19:24
  • I will mark as valid as certainly you answered it for my formulated DAO class, but I see I missed the way I formulated the question; rather than be DAO specific, I was looking for the correct granularity way to describe responsabilities. I will formulate it in another question. – ejaenv Jul 13 '11 at 08:20
  • I found my same DAO question formulated here (http://stackoverflow.com/questions/2643547/how-to-apply-single-responsibility-principle-to-a-service-class). It was answered similar as you. In addition, the answers says that the SRP is not broken as long as the DAO is only focused on one data object. – ejaenv Jul 13 '11 at 08:48