Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Does the single responsibility principal promote flexibility? If not what are the other methods to make your classes more flexible?

By flexibility I mean, a class is able to function correctly at any time with little (or even with no) hassle.

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

Yes. A class that does one thing can be reused in other contexts easily where that responsibilty is necessary; if a class has more than one responsibility, in most cases those other responsibilities create dependencies that might be hard to satisfy in a different context.

Example: A class that does some calculation is easy to reuse. A class that does the calculation and stores the result to the database is hard to reuse in a context without a database, even if you only need the calculation part. Maybe the constructor requires a database connection object, maybe the calculation does the update implicitely.

share|improve this answer
    
+1 for the very practical example! –  mikera Jun 21 '11 at 13:21
add comment

It's an interesting way to define flexibility. But there's a lot of truth in it, i.e. if a class is robust (as I would call it), you're flexible in using it.

The single responsibility principle doesn't explicitly "promote" robustness. It just say, that a given class should be designed for a single task. Within tighter constraints, robustness is much easier to achieve (as an anology: a swiss army knife is less robust that an Opinel).
The more narrow the task, the less unnecessary implicit assumptions you make, that could break the class when they don't hold, because the client code had no idea they were there.

However the SRP focuses on the inner qualities of a class. What you are interested in is the outer behavior. Such behavior should be abstracted according to DIP and decomposed according to ISP. This is where flexibility comes from: You can plug in implementations at will and implementations are easy to make (no need to implement 50 methods).

share|improve this answer
1  
this is a great explanation, and so the answer to my question is "it is but indirectly". –  Shaheer Jun 21 '11 at 11:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.