Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 12 months ago.
The community reviewed whether to reopen this question 12 months ago and left it closed:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
想补充一些我在其他答案中没有看到的观点。
虽然传统上“工厂”意味着“创建对象”,但我喜欢将其更广泛地视为“返回一个行为符合我预期的对象”。我不应该总是知道它是否是一个全新的对象,事实上我可能不在乎。因此,在适当的情况下,您可能会避免使用“创建...”名称,即使这就是您现在实现它的方式。
Guava 是一个很好的工厂命名思想存储库。它正在流行一种很好的 DSL 风格。示例:
Wanted to add a couple of points I don't see in other answers.
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.
Guava is a good repository of factory naming ideas. It is popularising a nice DSL style. examples:
“Create”和“make”很短,相当令人回味,并且与我能想到的其他命名模式无关。我也经常看到这两种情况,并怀疑它们可能是“事实上的标准”。我会选择一个并至少在项目中一致地使用它。 (看看我自己当前的项目,我似乎使用“make”。我希望我保持一致......)
避免“build”,因为它更适合Builder模式,避免“product”,因为它唤起生产者/消费者。
为了真正继续该模式的“工厂”名称的隐喻,我会受到“制造”的诱惑,但这个词太长了。
"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.
我认为它源于“创建一个对象”。然而,在英语中,“创造”一词与“导致形成,作为一种不会自然进化或不是通过普通过程制造的独特事物”的概念相关联,以及“从一个人自己的思想或想象力,作为一件艺术品或一项发明。”因此,“创建”似乎不是正确的用词。另一方面,“制造”的意思是“通过塑造或改变材料、组合部件等来实现”。例如,您不是创造一件衣服,而是制作一件衣服(对象)。因此,在我看来,“make”的意思是“生产;生产;生产”。导致存在或发生; “带来”对于工厂方法来说是一个更好的词。
I think it stems from “to create an object”. However, in English, the word “create” is associated with the notion “to cause to come into being, as something unique that would not naturally evolve or that is not made by ordinary processes,” and “to evolve from one's own thought or imagination, as a work of art or an invention.” So it seems as “create” is not the proper word to use. “Make,” on the other hand, means “to bring into existence by shaping or changing material, combining parts, etc.” For example, you don’t create a dress, you make a dress (object). So, in my opinion, “make” by meaning “to produce; cause to exist or happen; bring about” is a far better word for factory methods.
我将其称为
UriFactory.Create()
其中,
UriFactory
是类类型的名称,它提供创建的方法Uri
实例。并且
Create()
方法会根据您的规范中的变化进行重载。I'd call it
UriFactory.Create()
Where,
UriFactory
is the name of the class type which is provides method(s) that createUri
instances.and
Create()
method is overloaded for as many as variations you have in your specs.部分是约定,部分是语义。
工厂方法(由传统的
create
发出信号)应该调用适当的构造函数。如果我看到buildURI
,我会假设它涉及一些计算或部件组装(而且我不认为涉及工厂)。当我看到generateURI
时,我想到的第一件事就是随机生成一些东西,比如新的个性化下载链接。它们并不完全相同,不同的词会产生不同的含义;但其中大多数都没有被约定俗成。Partly convention, partly semantics.
Factory methods (signalled by the traditional
create
) should invoke appropriate constructors. If I sawbuildURI
, 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 sawgenerateURI
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.我喜欢新的。对我来说,
更好
读起来比翻译成英语 ,我们有 foo 是一个新的 foo 或 foo 是创建 foo。虽然我不是语法专家,但我很确定后者在语法上是不正确的。
I like new. To me
reads better than
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.
我要指出的是,我已经见过所有动词,但在某些库或其他库中使用了 Produce,所以我不会将 create 称为通用约定。
现在,“创造”对我来说确实听起来更好,唤起了该行动的确切含义。
所以是的,这是一个(文学)品味的问题。
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.
我个人喜欢
instantiate
和instantiateWith
,但这只是因为我的 Unity 和 Objective C 经验。 Unity引擎内部的命名约定似乎围绕着instantiate
这个词来通过工厂方法创建实例,而Objective C似乎喜欢with
来指示参数是什么是。只有当该方法位于要实例化的类中时,这才真正有效(并且在允许构造函数重载的语言中,这并不是什么“东西”)。只是简单的旧 Objective C 的
initWith
也是一个好东西!Personally I like
instantiate
andinstantiateWith
, but that's just because of my Unity and Objective C experiences. Naming conventions inside the Unity engine seem to revolve around the wordinstantiate
to create an instance via a factory method, and Objective C seems to likewith
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!一些随机的想法:
“创建”比大多数其他词更适合该功能。我能立即想到的下一个最好的词是“构建”。过去,“Alloc”(分配)可能会在类似的情况下使用,这反映出在 C 等语言中对数据块的重视程度高于对对象的重视。
“Create”是一个简短的单词,具有清晰直观的含义意义。在大多数情况下,当人们想要创造一些东西时,他们可能只是选择它作为第一个、最明显的词。这是一种常见的命名约定,“对象创建”是描述...创建对象的过程的常见方式。
“Construct”很接近,但通常用于描述创建对象过程中的特定阶段(分配/新建、构造、初始化...)
“Build”和“Make”是与编译代码相关的过程的常用术语,因此具有不同的含义对于程序员来说,它意味着一个包含许多步骤并且可能包含大量磁盘活动的过程。然而,工厂“构建”某些东西的想法是一个明智的想法 - 特别是在构建复杂的数据结构或以某种方式组合许多单独的信息的情况下。
“生成”对我来说意味着一种计算,用于从输入生成一个值,例如生成哈希码或随机数。
“生产”、“生成”、“构造”的输入/读取时间比“创建”要长。从历史上看,程序员喜欢使用短名称来减少输入/读取。
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 在“Effective Java”建议遵循以下命名约定
Joshua Bloch in "Effective Java" suggests the following naming conventions