I am wondering about correct definition for such construction:
class A {
public static A create() {
return new A();
}
private A() {
}
}
In Effective Java (Item 1) and on wikipedia article I found that this is called Static Factory Method (some kind of Factory Method).
But during reading of Refactoring to Patterns (Chapter 6) I met the same construction called Creation Method. Also, there is a note that it should not be messed up with a Factory Method pattern.
Where truth is?
-
One approach is to call parameterless methods creation methods and parameterized (for example by an enum) - factory methods. In the sence that a factory is more powerful and can create objects of different types.
If you use a parameterless method you have to decide elsewhere which class' method to call. With a parameterized method you pass this logic to the method itself. So the latter (factory) also decides by itself which class object to create.
Andrey Vityuk : Sorry, did not get the actual difference between creation methods and factory methods from your post. Could you explain it once more?sharptooth : To call a creation method you need to decide which class' method to call - B::Create() or C::Create(). With factory method you just class F::Create( parameter ) where a parameter is some enum value for example and the fcatory decides itself which class object to create. -
Well, terminology often varies between authors, so I wouldn't worry too much about this.
I suppose, however, that "Refactoring to Patterns" warns against calling this a "factory method", because there is the factory method pattern. Since the factory method pattern is more than just a factory method, they propose a different name to avoid confusion.
I guess you could also call it a "simple static factory", but that's a bit wordy (and non-standard).
-
Have a read of this discussion of Factory Method.
FactoryMethodPattern is different from FactoryMethod or CreationMethod.
Andrey Vityuk : Thanks, now it makes sense.
0 comments:
Post a Comment