mercredi 28 janvier 2015

Does the single responsibility principle promote many small classes over a logical grouping?

I understand the basic concept and every example out there shows two completely different concerns to illustrate the point but my question is regarding related, but separate, concerns.


Take a simple calculator. It has the following 4 methods:



  1. Add

  2. Subtract

  3. Multiply

  4. Divide


I think most developers would create a class with 4 methods. But that breaks SRP right? Should this be broken into 4 separate classes, one for each operation?


Maybe yes, because lets say we have overloads for each method. If we want to perform operations on int, float, and decimal our method list expands to 12. So maybe separate classes would be better. This makes a bit more sense (to separate them into 4 classes) but should you do that when there are no overloads and only 4 methods?


Lets add a few more methods:



  1. Sin

  2. Cos

  3. Tan


Do you add these 3 methods to the 4 above? Do you create a separate class for each one? Do you group the 4 above in a Basic class and these 3 in a Geometry class?


Aucun commentaire:

Enregistrer un commentaire