如何命名类似工厂的方法?

发布于 2024-09-12 06:20:29 字数 1703 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

疏忽 2024-09-19 06:20:30

想补充一些我在其他答案中没有看到的观点。

  1. 虽然传统上“工厂”意味着“创建对象”,但我喜欢将其更广泛地视为“返回一个行为符合我预期的对象”。我不应该总是知道它是否是一个全新的对象,事实上我可能不在乎。因此,在适当的情况下,您可能会避免使用“创建...”名称,即使这就是您现在实现它的方式。

  2. Guava 是一个很好的工厂命名思想存储库。它正在流行一种很好的 DSL 风格。示例:

     Lists.newArrayListWithCapacity(100);
     ImmutableList.of("你好", "世界");
    

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");
    
懷念過去 2024-09-19 06:20:30

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

指尖微凉心微凉 2024-09-19 06:20:30

我认为它源于“创建一个对象”。然而,在英语中,“创造”一词与“导致形成,作为一种不会自然进化或不是通过普通过程制造的独特事物”的概念相关联,以及“从一个人自己的思想或想象力,作为一件艺术品或一项发明。”因此,“创建”似乎不是正确的用词。另一方面,“制造”的意思是“通过塑造或改变材料、组合部件等来实现”。例如,您不是创造一件衣服,而是制作一件衣服(对象)。因此,在我看来,“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.

日暮斜阳 2024-09-19 06:20:30

我将其称为 UriFactory.Create()

其中,

UriFactory 是类类型的名称,它提供创建的方法Uri 实例。

并且 Create() 方法会根据您的规范中的变化进行重载。

public static class UriFactory
{
    //Default Creator
    public static UriType Create() 
    {
    }

    //An overload for Create()
    public static UriType Create(someArgs) 
    {
    }
}

I'd call it UriFactory.Create()

Where,

UriFactory is the name of the class type which is provides method(s) that create Uri instances.

and Create() method is overloaded for as many as variations you have in your specs.

public static class UriFactory
{
    //Default Creator
    public static UriType Create() 
    {
    }

    //An overload for Create()
    public static UriType Create(someArgs) 
    {
    }
}
萤火眠眠 2024-09-19 06:20:30

部分是约定,部分是语义。

工厂方法(由传统的create 发出信号)应该调用适当的构造函数。如果我看到 buildURI,我会假设它涉及一些计算或部件组装(而且我不认为涉及工厂)。当我看到 generateURI 时,我想到的第一件事就是随机生成一些东西,比如新的个性化下载链接。它们并不完全相同,不同的词会产生不同的含义;但其中大多数都没有被约定俗成。

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.

暮光沉寂 2024-09-19 06:20:30

我喜欢新的。对我来说,

var foo = newFoo();

更好

var foo = createFoo();

读起来比翻译成英语 ,我们有 foo 是一个新的 foo 或 foo 是创建 foo。虽然我不是语法专家,但我很确定后者在语法上是不正确的。

I like new. To me

var foo = newFoo();

reads better than

var foo = createFoo();

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.

傲鸠 2024-09-19 06:20:30

我要指出的是,我已经见过所有动词,但在某些库或其他库中使用了 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.

ゃ人海孤独症 2024-09-19 06:20:30

我个人喜欢 instantiateinstantiateWith,但这只是因为我的 Unity 和 Objective C 经验。 Unity引擎内部的命名约定似乎围绕着instantiate这个词来通过工厂方法创建实例,而Objective C似乎喜欢with来指示参数是什么是。只有当该方法位于要实例化的类中时,这才真正有效(并且在允许构造函数重载的语言中,这并不是什么“东西”)。

只是简单的旧 Objective C 的 initWith 也是一个好东西!

Personally I like instantiate and instantiateWith, but that's just because of my Unity and Objective C experiences. Naming conventions inside the Unity engine seem to revolve around the word instantiate to create an instance via a factory method, and Objective C seems to like with 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!

对不⑦ 2024-09-19 06:20:29

一些随机的想法:

  • “创建”比大多数其他词更适合该功能。我能立即想到的下一个最好的词是“构建”。过去,“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.

缺⑴份安定 2024-09-19 06:20:29

Joshua Bloch 在“Effective Java”建议遵循以下命名约定

valueOf — 返回一个具有相同值的实例
作为其参数。这种静态工厂有效地
类型转换方法。

ofvalueOf 的简洁替代方案,由 EnumSet 普及(第 32 项)。

getInstance — 返回由参数描述的实例
但不能说具有相同的价值。在单例的情况下,
getInstance 不带任何参数并返回唯一的实例。

newInstance — 与 getInstance 类似,但 newInstance 保证
返回的每个实例都与其他实例不同。

getType — 与 getInstance 类似,但当工厂方法位于
不同的班级。 Type表示返回的对象类型
工厂方法。

newType — 与 newInstance 类似,但当工厂方法位于
不同的班级。 Type表示返回的对象类型
工厂方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文