From time to time, there are questions which I can be answered per se with one short sentence. A detailed answer would be more valuable for both the community and the person asking the question, but a short one may contain enough information if the person asking the question is ready to make an effort to search for details on his own.
Basic example:
Question:
I notice that a few classes have a lot of code related to the creation of the instances of those classes, rather than the logic itself. For example, one class contains three short instance methods, but eight static methods which deal with its initialization.
Having all those static methods in the class itself it distracting. Moreover, I don't understand how is it possible to mock those methods for unit tests.
What should I do?
Quick and short answer:
You may be interested in factory design pattern.
For community, such answer is not particularly valuable. It doesn't explain neither why a factory is useful here, nor how to use it, nor if this is the only solution to the OP's problem. From a real answer, I would expect:
The general explanation of what factory pattern is about,
Additional benefits of the factory pattern the author of the question may not think about, such as the possibility to create instances of different types (a factory which creates either
Cat inherits Animal
orDog inherits Animal
depending on some criteria),The explanation of the difference between a factory and an abstract factory (especially the relation with mocking, mentioned in the question),
The mention of the builder pattern,
An example (piece of code) of a factory pattern applied to a class (and how the factory pattern can be mocked),
Eventually additional hints, such as the usage of method chaining in a builder for a more fluent interface.
On the other hand, the question is actually answered, and if the author of the question makes an effort of searching for factory pattern, he will find everything he needs to successfully get rid of those static methods in his class.
If I can post a quick and short answer, but don't have time for a long, detailed answer (and don't have time to edit it in the near future into a detailed answer), what should I do:
- Post an answer?
- Post it as a comment?
- Post an answer as a community wiki, expecting other people to make it more detailed (I hardly doubt this will work; other persons would rather post their own answer instead)?
- Avoid posting anything and make the person wait until somebody else answers the question?