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.
|
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. |
||||
|
|
|
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. |
|||
|
|
|
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). 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). |
|||||
|