I guess that most factory-like methods start with create
. But why are they called "create"? Why not "make", "produce", "build", "generate" or something else? Is it only a matter of taste? A convention? Or is there a special meaning in "create"?
createURI(...)
makeURI(...)
produceURI(...)
buildURI(...)
generateURI(...)
Which one would you choose in general and why?
Joshua Bloch in "Effective Java" suggests the following naming conventions
I'd call it
UriFactory.Create()
Where,
UriFactory
is the name of the class type which is provides method(s) that createUri
instances.and
Create()
method is overloaded for as many as variations you have in your specs.I like new. To me
reads better than
Translated to english we have foo is a new foo or foo is create foo. While I'm not a grammer expert I'm pretty sure the latter is grammatically incorrect.
Personally I like
instantiate
andinstantiateWith
, but that's just because of my Unity and Objective C experiences. Naming conventions inside the Unity engine seem to revolve around the wordinstantiate
to create an instance via a factory method, and Objective C seems to likewith
to indicate what the parameter/s are. This only really works well if the method is in the class that is going to be instantiated though (and in languages that allow constructor overloading, this isn't so much of a 'thing').Just plain old Objective C's
initWith
is also a good'un!Factory method doesn't dictate on method name. You can have as many methods you want in your factory, provided all of them return the object from same family.
For more details, visit the url http://xeon2k.wordpress.com
Wanted to add a couple of points I don't see in other answers.
Although traditionally 'Factory' means 'creates objects', I like to think of it more broadly as 'returns me an object that behaves as I expect'. I shouldn't always have to know whether it's a brand new object, in fact I might not care. So in suitable cases you might avoid a 'Create...' name, even if that's how you're implementing it right now.
Guava is a good repository of factory naming ideas. It is popularising a nice DSL style. examples: