Design Patterns – How is DRY Principle Related to SRP at Class Level

design-patternsdrysingle-responsibility

In other words, is DRY (don't repeat yourself) applied at a class level a subset of SRP (single responsibilty principle)?

What I mean is that while SRP states that each class should have only a single responsibility ( ie. class should only have one reason to change ), is it the application of DRY at class level that prevents two classes from having the same responsibility ( thus it is DRY that prevents the repetition/duplication of same responsibility in two different classes )?

Thank you

Best Answer

What I mean is that while SRP states that each class should have only a single responsibility ( ie. class should only have one reason to change ), is it the application of DRY at class level that prevents two classes from having the same responsibility ( thus it is DRY that prevents the repetition/duplication of same responsibility in two different classes )?

No. You can still not repeat yourself but violate single responsibility. Likewise, you can repeat yourself but classes only have a single (duplicated, but subtly different) responsibility.

While the two guidelines can and will tend to overlap, they are orthogonal concepts.