I'm used to creating Singletons to implement a globally shared state, and I'm reading about how Singletons aren't testable and that the Factory pattern should be used instead. The problem I'm having is that I can't see where would be a good place to create the Factory object, in order for it to be accessible everywhere.
Most online examples of the Factory pattern show the object creation in the Main
method. I'm programming in the context of a Web application which is packaged in a WAR.
For example, let's say I have ServletA
and ServletB
. ServletA
creates an instance of the FactoryClass
but when ServletB
is invoked, it won't have access to the FactoryClass
's instance.
I'm new to trying to implement the Factory pattern and I want to try do it "the right way", instead of using a Singleton. Also, I want to be able to write more testable code.
Aucun commentaire:
Enregistrer un commentaire