| Author |
different types of factories - really??
|
nibla jose
Greenhorn
Joined: May 25, 2009
Posts: 28
|
|
Scenarios...
1. Simple factory (I have avsolutely no idea whether there is one like this) - a static method in some class returning different implementations of a single interface based on an input criteria (usually something in the genre of an Enum)
participants...
a. Interface SF1
b. impl1 - SFImpl1
c. impl2 - SFImpl2
d. The class containing (probably) a static method which contains a lot of if else statements
2. Factory method - a special type of template method whereas the template method itself deals only with object creation rather than anything else. Majority of the other methods in the class works on the product the factory method creates.
participants..
Abstract class Fm1 with the template or factory method
Impl1 - FmImpl1
Impl2 - FmImpl2
Clarifications...
1. Are both the above types are called factories?
2. On what basis do we decide that any one of them is a pattern or not?
3. Doesn't the second one always need the help of first to use it??
4. Which class/method among the above is the real factory?
5. Also is the factory a method or a class?
Thanks in advance for all replies..
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
nibla jose wrote:1. Are both the above types are called factories?
2. On what basis do we decide that any one of them is a pattern or not?
3. Doesn't the second one always need the help of first to use it??
4. Which class/method among the above is the real factory?
5. Also is the factory a method or a class?
1) Some people call the first one a factory. I prefer the name "(static) creation method".
2) Basically, something becomes a pattern by enough people in the community agreeing that it is.
3) Does it? Why?
4) None. The name "Factory Method" refers to the whole design idea, not to single classes. Note that there is another pattern, "Abstract Factory", where there actually are classes that can be called factories.
5) Mu. see 4)
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
nibla jose
Greenhorn
Joined: May 25, 2009
Posts: 28
|
|
Ilja,
On the third point. The factory method pattern, gives us the different implementations differing mainly in the method to create the object. How does a client code use this?
a. By directly instantiating an implementation? It is one possibility. But isn't that a tightly coupled one?
b. According to me, we may have to use the first so called 'simple factory' to get the different implementation classes to the abstract factory method? Right?
Do you see any other possibilities?
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Sometimes, directly instantiating the "factory object" is quite appropriate. In fact, there really isn't that big a difference in coupling between
createCarFactory("GM")
and
new GmCarFactory()
If your client needs to use a specific factory, just instantiate it.
If it doesn't, some form of Depency Injection is typically called for.
|
 |
nibla jose
Greenhorn
Joined: May 25, 2009
Posts: 28
|
|
|
Thanks for the answers.. It makes sense..
|
 |
 |
|
|
subject: different types of factories - really??
|
|
|