享元设计模式名称的由来是什么?

发布于 2024-10-06 15:45:32 字数 282 浏览 2 评论 0原文

我的母语不是英语,并且试图更好地理解该设计模式,我对用于命名模式的单词的起源感兴趣。这个名字的动机是什么?

据我所知,flyweight设计模式涉及一种使用共享来支持大量细粒度对象的方法。

然而,我找到的特定词(蝇量级)的定义是指重量小于 112 磅的拳击类别。

那么,为什么要以这种方式调用该模式呢?

抱歉,如果这看起来很愚蠢,但我真的不知道。

I'm a non native English speaker, and trying to grasp a better understanding of that design pattern, I'm interested in the origin of that word for naming a pattern. What are the motivations for that name?

As far I know the flyweight design pattern is concerned with a way to support large number of fine-grained objects using sharing.

However, the definitions I'd found for that specific word (flyweight) refers to the boxing category of weight of less than 112 pounds.

So, why the pattern is called in this way?

Sorry if this seems foolish, but I really have no clue.

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

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

发布评论

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

评论(4

独﹏钓一江月 2024-10-13 15:45:33

享元模式用于在需要创建大量相似对象时最大限度地减少内存使用量。它通过共享实例来实现这一点。

正如您提到的,该名称源自重量分类,但指的是少量内存。也就是说,记忆力=体重。

The flyweight pattern is used to minimize the amount of memory used when you need to create a large number of similar objects. It accomplishes this by sharing instances.

The name derives from the weight classification as you mention but refers to the little amount of memory. That is, memory = weight.

ぇ气 2024-10-13 15:45:33

轻量级(或称蝇量级)表明创建的类实现尽可能少的代码(并保存尽可能少的数据)来完成其工作。

因此,创建(并实例化、传递它们)和维护它们就更容易了——就像举起一个基本上空的盒子一样。

这与可能包装大量数据并包含大量代码的重量级类形成对比。

Lightweight -- or flyweight -- suggests that the classes created implement as little code (and hold as little data) as possible to do their job.

Creating (and instantiating, and passing them around) and maintaining them is therefore easier --like lifting a basically empty box.

This in contrast to a heavyweight class that may wrap a lot of data and comprise a lot of code.

旧人九事 2024-10-13 15:45:33

来自 GoF 书籍

享元是可以在多个上下文中同时使用的共享对象。享元在每个上下文中充当独立对象 - 它与非共享对象的实例没有区别。享元无法对其运作环境做出假设。这里的关键概念是内在状态和外在状态之间的区别。内在状态存储在享元中;它由独立于享元上下文的信息组成,从而使其可共享。外部状态取决于享元的上下文并随其变化,因此无法共享。

- 埃里希·伽玛;赫尔姆,理查德;约翰逊,拉尔夫;弗利塞德斯,约翰.设计模式(Addison-Wesley 专业计算系列)

中的flyweight飞轮用于存储机械能以便稍后共享(很像电感器或蓄电池)。

此外,蝇量级是轻量级拳击类别。

考虑到 GoF 给出的定义,我推测该术语最初被选为飞轮飞量级的隐喻。

然而,由于flyweight也意味着轻量级拳击类别,他们可能将其称为“flyweight”模式,而不是“飞轮”/“电感器”/“累加器”模式,因为flyweight意味着两者一个共享对象轻量级对象

人们不禁会嘲笑机械蝇量级实际上非常重,这与该术语用于轻量级拳击的事实相矛盾。

GoF还表示

享元对象的概念首次在 Interviews 3.0 [CL90] 中作为一种设计技术进行描述和探索。

以及您可以在此处找到该论文。作者没有解释为什么这样命名,但从论文中可以清楚地看出,他们正在定义一个轻量级、可共享的对象。

From the GoF book:

A flyweight is a shared object that can be used in multiple contexts simultaneously. The flyweight acts as an independent object in each context— it’s indistinguishable from an instance of the object that’s not shared. Flyweights cannot make assumptions about the context in which they operate. The key concept here is the distinction between intrinsic and extrinsic state. Intrinsic state is stored in the flyweight; it consists of information that’s independent of the flyweight’s context, thereby making it sharable. Extrinsic state depends on and varies with the flyweight’s context and therefore can’t be shared.

- Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John. Design Patterns (Addison-Wesley Professional Computing Series)

A flyweight in a flywheel is used to store mechanical energy to be shared later (much like an electrical inductor or an accumulator).

Additionally, flyweight is a light-weight boxing category.

Considering the definition given by the GoF, I surmise the term was originally chosen as a metaphor for a flywheel flyweight.

However, since flyweight also means lightweight boxing category, they probably called it the "flyweight" pattern instead of the "flywheel"/"inductor"/"accumulator" pattern because flyweight means both a shared object AND a lightweight object.

One can't help but laugh at the irony that mechanical flyweights are actually very heavy, in contradiction to the fact that the term is used for lightweight boxing.

The GoF also states that

The concept of flyweight objects was first described and explored as a design technique in Interviews 3.0 [CL90].

and that paper you can find here. The authors don't explain why they named it that, but it's clear from the paper that they are defining a lightweight, sharable object.

独自←快乐 2024-10-13 15:45:32

蝇量级是一个拳击类别,适合体重较轻的人。

享元模式适用于“轻量级”对象(尽管其中有很多)。

Flyweight is a boxing category, for light weight people.

Flyweight pattern is for "light weight" objects (though many of them).

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