How to name factory like methods?

后端 未结 11 1892
别跟我提以往
别跟我提以往 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: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");
      

提交回复
热议问题