How to name factory like methods?

后端 未结 11 1890
别跟我提以往
别跟我提以往 2021-01-29 19:41

I guess that most factory-like methods start with create. But why are they called \"create\"? Why not \"make\", \"produce\", \"build\", \"generate\" or something el

相关标签:
11条回答
  • 2021-01-29 20:35

    I'd point out that I've seen all of the verbs but produce in use in some library or other, so I wouldn't call create being an universal convention.

    Now, create does sound better to me, evokes the precise meaning of the action.

    So yes, it is a matter of (literary) taste.

    0 讨论(0)
  • 2021-01-29 20:37

    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.

    0 讨论(0)
  • 2021-01-29 20:39

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

    0 讨论(0)
  • 2021-01-29 20:42

    Partly convention, partly semantics.

    Factory methods (signalled by the traditional create) should invoke appropriate constructors. If I saw buildURI, I would assume that it involved some computation, or assembly from parts (and I would not think there was a factory involved). The first thing that I thought when I saw generateURI is making something random, like a new personalized download link. They are not all the same, different words evoke different meanings; but most of them are not conventionalised.

    0 讨论(0)
  • 2021-01-29 20:43

    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");
      
    0 讨论(0)
提交回复
热议问题