3

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.

Neel Basu
  • 12,638
  • 12
  • 82
  • 146
  • 5
    This is a discussion topic, not a question. Maybe move it to a more relevant location? – N_A Sep 06 '11 at 17:17
  • 1
    I agree with mydogisbox. This is one of those "it depends" questions. – Chad La Guardia Sep 06 '11 at 17:17
  • any reason it has to be a singleton and can't just be a standalone factory function? – Doug T. Sep 06 '11 at 17:20
  • Factory must be accessible from all corners that requires a product to be produced. so either make it global/Singleton or pass in in arguments and chain. and there will be a lot of parallel chains. and as I previously said `Passing a Factory in Parameter` How it sounds ? and When you make long chains of passing same object (sometimes Parallel chains exists) its even more difficult to test. – Neel Basu Sep 06 '11 at 17:30
  • 1
    Why does passing the factory to the classes that need it look "odd"? It's simply expressing those classes' dependency on the factory: useful information, that making it global will hide. – Mike Seymour Sep 06 '11 at 18:21
  • 2
    It also makes the classes *easier* to test - the test harness can pass its own implementation of the factory, which can check that it's being used correctly, without introducing a dependency on the "real" factory. – Mike Seymour Sep 06 '11 at 18:23

3 Answers3

3

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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Please check the Last part of the Edit – Neel Basu Sep 06 '11 at 17:51
  • 1
    @NeelBasu, I saw that, but I don't believe that Singleton is the only answer or that it's usually the best answer. Global access shouldn't be a crutch to support poor OO design. If you're creating the factory far from the place where it's used, that may indicate a need for a change in the way your objects are connected to each other. – Caleb Sep 06 '11 at 18:06
2

I'll side with Doug T.

You can very easily create a static function within a class which can be used to create an instance of that class (factory function). Better yet, you can create a factory class, and make the static function in the factory class generate the desired object. C# actually provides static classes where all members and functions are static for purposes like this - it's essentially a singleton anyway.

The point here is, whatever you do, it may make more sense to simply pass a function pointer to a factory function as your parameter. You'll have flexibility with where to store your creation logic then, and you can choose to avoid singletons/extra classes altogether.

(PS: I think singletons are a good pattern, but if you over-use them they become an anti-pattern because they reduce a design to something resembling just having global functions and data).

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
  • Isn't Static and Singleton more or less same ? and a factory must reach every end that require products to be produced so. the chain of passing and carrying the factory is not an unidirectional chain. it gets distributed very easily. so it will make problems while testing. – Neel Basu Sep 06 '11 at 17:39
  • 1
    @NeelBasu statics are static. You know they are static because that's what it says in the type data. Singletons pretend not be static but actually have internal logic to ensure you only ever use one instance (and use that instance every time). Singletons emulate static behaviour so use statics instead. – Raynos Sep 06 '11 at 17:42
  • @Neel Basu: Static and Singleton are vaguely conceptually similar. Your comment about the chain is highly unclear. – Beta Sep 06 '11 at 17:50
  • I am sorry if its unclear. I am trying again to make it clear.say if A,B,C,D,E all five needs factory F. C and D is instantiated by B which is instantiated by A and E is instantiated by D. and A is instantiated on the entry point of the program. so the Factory needs to be passed to A in its creation and then again it should be passed to B such that B can use it. A->B, B->C, B->D and D->E. its not limited to 5 (A .. E) it may even grow larger then the chain doesn't remain an single chain rather it becomes a tree. then it becomes even harder to test. – Neel Basu Sep 06 '11 at 18:33
0

No you shouldn't, in fact singleton pattern is often seen almost as an antipattern from someone.

The best thing would be to rely on some IOC container avoiding both singleton both factory pattern.

Simone
  • 2,261
  • 2
  • 19
  • 27
  • that someone is not God. and Someone says its an anti-pattern is not a Good reasoning – Neel Basu Sep 06 '11 at 17:23
  • well maybe not god itself but someone more reliable than me and you – Simone Sep 06 '11 at 17:25
  • singleton is an anti pattern for the obvious reason that make informations global; i think you agree with me saying that global variables are an antipattern – Simone Sep 06 '11 at 17:29
  • This response would be more helpful if you explained why. – Jordan Parmer Sep 06 '11 at 17:33
  • Because singleton pattern is only a way to do non-oop programming in a oop fashion; there's nothing object oriented in the singleton pattern – Simone Sep 06 '11 at 17:40
  • Singleton does not necessarily make information global. Like a chisel, it is a good tool when used correctly, and for some reason it is attractive to novices who like to use it for everything. – Beta Sep 06 '11 at 17:48
  • The fact that novice like it so much should worry everyone; i was part of that novices and then i saw the light! Singleton pattern is evil – Simone Sep 06 '11 at 17:50
  • @Neel: "Someone says its an anti-pattern is not a Good reasoning" - maybe not, but here are several people providing good reasoning: http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – Mike Seymour Sep 06 '11 at 18:26