2

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?

4 Answers 4

2

Have a read of this discussion of Factory Method.

FactoryMethodPattern is different from FactoryMethod or CreationMethod.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Sorry, did not get the actual difference between creation methods and factory methods from your post. Could you explain it once more?
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.
1

Creation Method is a static or non-static method that creates an instance of a class. Factory Method is a method defined and implemented in a class hierarchy and the creation is of a polymorphic nature.

Comments

0

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).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.