享元设计模式名称的由来是什么?
我的母语不是英语,并且试图更好地理解该设计模式,我对用于命名模式的单词的起源感兴趣。这个名字的动机是什么?
据我所知,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
享元模式用于在需要创建大量相似对象时最大限度地减少内存使用量。它通过共享实例来实现这一点。
正如您提到的,该名称源自重量分类,但指的是少量内存。也就是说,记忆力=体重。
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.
轻量级(或称蝇量级)表明创建的类实现尽可能少的代码(并保存尽可能少的数据)来完成其工作。
因此,创建(并实例化、传递它们)和维护它们就更容易了——就像举起一个基本上空的盒子一样。
这与可能包装大量数据并包含大量代码的重量级类形成对比。
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.
来自 GoF 书籍:
- 埃里希·伽玛;赫尔姆,理查德;约翰逊,拉尔夫;弗利塞德斯,约翰.设计模式(Addison-Wesley 专业计算系列)
中的flyweight飞轮用于存储机械能以便稍后共享(很像电感器或蓄电池)。
此外,蝇量级是轻量级拳击类别。
考虑到 GoF 给出的定义,我推测该术语最初被选为飞轮飞量级的隐喻。
然而,由于flyweight也意味着轻量级拳击类别,他们可能将其称为“flyweight”模式,而不是“飞轮”/“电感器”/“累加器”模式,因为flyweight意味着两者一个共享对象和轻量级对象。
人们不禁会嘲笑机械蝇量级实际上非常重,这与该术语用于轻量级拳击的事实相矛盾。
GoF还表示
以及您可以在此处找到该论文。作者没有解释为什么这样命名,但从论文中可以清楚地看出,他们正在定义一个轻量级、可共享的对象。
From the GoF book:
- 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
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.
蝇量级是一个拳击类别,适合体重较轻的人。
享元模式适用于“轻量级”对象(尽管其中有很多)。
Flyweight is a boxing category, for light weight people.
Flyweight pattern is for "light weight" objects (though many of them).