How to name factory like methods?

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");
    

"Create" and "make" are short, reasonably evocative, and not tied to other patterns in naming that I can think of. I've also seen both quite frequently and suspect they may be "de facto standards". I'd choose one and use it consistently at least within a project. (Looking at my own current project, I seem to use "make". I hope I'm consistent...)

Avoid "build" because it fits better with the Builder pattern and avoid "produce" because it evokes Producer/Consumer.

To really continue the metaphor of the "Factory" name for the pattern, I'd be tempted by "manufacture", but that's too long a word.


Some random thoughts:

  • 'Create' fits the feature better than most other words. The next best word I can think of off the top of my head is 'Construct'. In the past, 'Alloc' (allocate) might have been used in similar situations, reflecting the greater emphasis on blocks of data than objects in languages like C.

  • 'Create' is a short, simple word that has a clear intuitive meaning. In most cases people probably just pick it as the first, most obvious word that comes to mind when they wish to create something. It's a common naming convention, and "object creation" is a common way of describing the process of... creating objects.

  • 'Construct' is close, but it is usually used to describe a specific stage in the process of creating an object (allocate/new, construct, initialise...)

  • 'Build' and 'Make' are common terms for processes relating to compiling code, so have different connotations to programmers, implying a process that comprises many steps and possibly a lot of disk activity. However, the idea of a Factory "building" something is a sensible idea - especially in cases where a complex data-structure is built, or many separate pieces of information are combined in some way.

  • 'Generate' to me implies a calculation which is used to produce a value from an input, such as generating a hash code or a random number.

  • 'Produce', 'Generate', 'Construct' are longer to type/read than 'Create'. Historically programmers have favoured short names to reduce typing/reading.


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.