开发者

What is the reason for the name of the Flyweight Design Pattern?

开发者 https://www.devze.com 2023-01-29 02:51 出处:网络
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

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-grai开发者_如何学Cned 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.


Flyweight is a boxing category, for light weight people.

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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号