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.

6
  • 5
    This is a discussion topic, not a question. Maybe move it to a more relevant location?
    – N_A
    Sep 6, 2011 at 17:17
  • 1
    I agree with mydogisbox. This is one of those "it depends" questions. Sep 6, 2011 at 17:17
  • any reason it has to be a singleton and can't just be a standalone factory function?
    – Doug T.
    Sep 6, 2011 at 17:20
  • 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. Sep 6, 2011 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. Sep 6, 2011 at 18:23

3 Answers 3

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.

2
  • Please check the Last part of the Edit
    – Neel Basu
    Sep 6, 2011 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 6, 2011 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).

4
  • 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 6, 2011 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 6, 2011 at 17:42
  • @Neel Basu: Static and Singleton are vaguely conceptually similar. Your comment about the chain is highly unclear.
    – Beta
    Sep 6, 2011 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 6, 2011 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.

8
  • that someone is not God. and Someone says its an anti-pattern is not a Good reasoning
    – Neel Basu
    Sep 6, 2011 at 17:23
  • well maybe not god itself but someone more reliable than me and you
    – Simone
    Sep 6, 2011 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 6, 2011 at 17:29
  • This response would be more helpful if you explained why. Sep 6, 2011 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 6, 2011 at 17:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.