How to name factory like methods?

后端 未结 11 2092
别跟我提以往
别跟我提以往 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: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.

提交回复
热议问题