Single responsibility principle? Does it mean I can’t have more than one method in my class?

Today I have take a look on Single responsibility principle.

It states that every object should have a single responsibility.

It looks very logic and nice but does it mean that I can have only one method per class? Does it mean that every interface that I create need to have only one method for implement?

I am not quite sure that I understand this principle.What do you think?

6 Comments on Single responsibility principle? Does it mean I can’t have more than one method in my class?

  1. Ray
    April 15, 2009 at 6:02 pm (9 years ago)

    No, what it means is that your class should be all about a single concept. It could be a Data Transfer Object or a User Control Object. You should not take your Data Transfer Object and try to make it have GUI responsibilities. You would be coupling the GUI behavior to the DTO. It is better to pass the DTO off to another object that will display it. That’s the short explanation.

  2. Dr Herbie
    April 16, 2009 at 4:41 am (9 years ago)

    Replace the work ‘responsibility’ with the word ‘job’ to get a better idea of how to separate things out. For example a class that handles data persistence should do just that and not be responsible for imposting business rules on the data, or the class that imposes business rules for, say, customer names and addresses should not be responsible for rules regarding placing an order on a website.

    There are no hard and fast rules about this — different people will split the responsibilities out in different ways. It’s more important that the split is made, rather than specifically where the split is made.

  3. Radenko Zec
    April 16, 2009 at 5:00 am (9 years ago)

    But question is simple.Is one responsibility represents one method in class? So if in one class I have another method it is logical that other method have other responsibility?

  4. John
    April 18, 2009 at 8:47 pm (9 years ago)

    Responsibility does not equal method. It’s a higher level of abstraction. Here is an article on Responsibility-based modeling by Alistair Cockburn

    The notion of "responsibility" is best seen when one uses Class-Responsibility-Collaborator (CRC) cards, which Ward Cunningham and Kent Beck developed. The idea was to be able to say what a class does without speaking of its internal workings (ex. methods) during the early design stage. For example, in the MVC(Model2) pattern, the View has the "responsibility" of displaying information to the user. The POSA 1 book hac CRC cards with the patterns. If you go to Amazon,

    and search inside the book for "responsibility", you’ll be able to find some good examples.

    "One responsibility" is a guideline more than a rule. Robert Martin included it as the "O" in his SOLID principles, but I don’t know if he initiated the guideline. Even he will concede that it shouldn’t be applied universally. Back to the MVC example, the Controller not only accepts input from the user, but is also responsible for creating the view so it has multiple responsibilities.

  5. Radenko Zec
    April 18, 2009 at 9:12 pm (9 years ago)

    John great resources.Thanks very much.