An example of SRP would be File
, FileReader
and FileWriter
: there are many different ways to read from a file, and many different ways to write to a file. Therefore, these behaviors are composed by their own types (FileReader
for reading, FileWriter
for writing), allowing you to scale up (add new overloads) while still keeping modular.
So how do we determine when a type should be created to take care of a responsibility? Even if a behavior doesn't have any overloads, we can't be sure that'll stay true in time.
For example, a Game
that can start()
and stop()
. If each behavior had no overloads, it would be an overkill to create new types for each behavior. But if I don't create new types for each, and I wind up needing to create overloads, I'd be required to restructure my type to abide by SRP, breaking code in the process.
Does this mean we should create a new type for each kind of behavior? Wouldn't this clutter the namespace?
Aucun commentaire:
Enregistrer un commentaire