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?

  • http://initializecomponent.blogspot.com/ Ray

    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.

  • Dr Herbie

    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.

  • http://blog.developers.ba/ Radenko Zec

    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?

  • John

    Responsibility does not equal method. It’s a higher level of abstraction. Here is an article on Responsibility-based modeling by Alistair Cockburn
    http://web.archive.org/web/20040404054737/members.aol.com/humansandt/techniques/responsibility.htm

    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, http://www.amazon.com/Pattern-Oriented-Software-Architecture-System-Patterns/dp/0471958697/ref=sr_1_1?ie=UTF8&s=books&qid=1240152193&sr=8-1

    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.

  • http://blog.developers.ba/ Radenko Zec

    John great resources.Thanks very much.

  • http://blogpanjkov.qsh.eu/ Dragan Panjkov

    You can check out series of posts by Jeremy Miller in MSDN magazine in which he covers Patterns in practice, as well as SOLID principles http://msdn.microsoft.com/en-us/magazine/cc720886.aspx . Also, check out his blog at http://codebetter.com/blogs/jeremy.miller/