How to name factory like methods?

2019-03-07 19:57发布

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?

11条回答
【Aperson】
2楼-- · 2019-03-07 20:38

Joshua Bloch in "Effective Java" suggests the following naming conventions

valueOf — Returns an instance that has, loosely speaking, the same value as its parameters. Such static factories are effectively type-conversion methods.

of — A concise alternative to valueOf, popularized by EnumSet (Item 32).

getInstance — Returns an instance that is described by the parameters but cannot be said to have the same value. In the case of a singleton, getInstance takes no parameters and returns the sole instance.

newInstance — Like getInstance, except that newInstance guarantees that each instance returned is distinct from all others.

getType — Like getInstance, but used when the factory method is in a different class. Type indicates the type of object returned by the factory method.

newType — Like newInstance, but used when the factory method is in a different class. Type indicates the type of object returned by the factory method.

查看更多
Luminary・发光体
3楼-- · 2019-03-07 20:42

I'd call it UriFactory.Create()

Where,

UriFactory is the name of the class type which is provides method(s) that create Uri instances.

and Create() method is overloaded for as many as variations you have in your specs.

public static class UriFactory
{
    //Default Creator
    public static UriType Create() 
    {
    }

    //An overload for Create()
    public static UriType Create(someArgs) 
    {
    }
}
查看更多
一纸荒年 Trace。
4楼-- · 2019-03-07 20:45

I like new. To me

var foo = newFoo();

reads better than

var foo = createFoo();

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.

查看更多
疯言疯语
5楼-- · 2019-03-07 20:46

Personally I like instantiate and instantiateWith, but that's just because of my Unity and Objective C experiences. Naming conventions inside the Unity engine seem to revolve around the word instantiate to create an instance via a factory method, and Objective C seems to like with 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!

查看更多
对你真心纯属浪费
6楼-- · 2019-03-07 20:46

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

查看更多
神经病院院长
7楼-- · 2019-03-07 20:48

Wanted to add a couple of points I don't see in other answers.

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

  2. Guava is a good repository of factory naming ideas. It is popularising a nice DSL style. examples:

    Lists.newArrayListWithCapacity(100);
    ImmutableList.of("Hello", "World");
    
查看更多
登录 后发表回答