I was thinking the same thing. In my case, the abundance of factories is caused by the "assembly for verification." For example, I have a constructor like this:
ParserBuilderFactoryImpl(ParserFactory psF) { ... }
Here I have a parser - the final class I need. The parser is built by calling methods in the builder. The creators (new to each parser to be built) are derived from the builder factory.
Now, what h..l is a ParserFactory? Ah, I'm glad you asked! To check the implementation of the parser builder, I need to call its method, and then see which parser creator is created. The only way to do this without breaking the encapsulation of a particular parser class that the builder creates is to set an intercept point immediately before creating the parser to see what is included in its constructor. Therefore, ParserFactory. This is just a way to observe in a unit test what is passed to the parser constructor.
I'm not quite sure how to solve this, but I have a feeling that it would be better for us to bypass classes rather than factories, and Java would be better if it could have the correct class methods, rather than static members.
Arkadiy
source share