Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton Pattern Controversy with Factory Pattern [closed]

Often I use factory Pattern when there are complexities around object creations and that complexities incorporates informations inaccessible (should not be accessible)to the creation scope or creation incorporates some mandatory unavoidable workarounds.

Often I make the Factory a Singleton cause there is no need to multiple factories. and passing the same factory to multiple classes looks so odd. passing a whole Factory in parameter

There have been a lot of controversies regarding Singleton Pattern. So Should I make Factory a Singleton still ?

The Factory needs to accessible to every corners that need the factory to produce some product . that requires passing that factory as an argument. and again passing that in a chain. and that chain will not be unidirectional. that will easily make branches. that will also lead to testing difficulties.

like image 878
Neel Basu Avatar asked Dec 05 '25 01:12

Neel Basu


1 Answers

A class needn't be a singleton to provide a shared instance, and the fact that you only have one instance doesn't mean that the class must be a singleton. If you must have no more than one instance, that's the place to use a singleton. There's nothing in the Factory pattern that requires that you have only one factory -- it's easy to imagine having several factories each configured differently and each creating differently configured objects.

like image 141
Caleb Avatar answered Dec 07 '25 14:12

Caleb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!