1/19
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What is the Flyweight pattern?
A pattern that uses sharing to support large numbers of fine-grained objects efficiently.
What problem does Flyweight solve?
It reduces memory usage when an application has huge numbers of very similar objects.
What is the definition of Flyweight?
Use sharing to support large numbers of fine-grained objects efficiently.
What is the motivating example in the slides?
A role-playing game with huge numbers of terrain objects like trees and mountains.
Why is memory a problem in the terrain example?
Because millions of terrain objects and repeated sprite images would consume too much memory.
When should Flyweight be used?
When an application uses a large number of objects and storage costs are high.
What is intrinsic state?
State stored inside the flyweight that is independent of context and can be shared.
What is extrinsic state?
State that depends on context, varies between uses, and cannot be shared.
In the terrain example, what is intrinsic state?
The sprite image, such as tree.png.
In the terrain example, what is extrinsic state?
The x and y position where the terrain is drawn.
Why is object identity an issue in Flyweight?
Because shared flyweights may be reused for conceptually different objects, so identity tests can be misleading.
What is the role of the FlyweightFactory?
It manages a pool of flyweights and returns an existing one when possible instead of creating duplicates.
What does the factory do if a requested flyweight already exists?
It returns the existing shared flyweight.
What does the factory do if a requested flyweight does not exist?
It creates it, stores it, and returns it.
What happens without Flyweight in the tree example?
One million Tree objects are created and tree.png is loaded one million times.
What happens with Flyweight in the tree example?
One shared terrain object is reused and tree.png is loaded only once.
What is the main benefit of Flyweight?
Reduced memory requirements.
What is one downside of Flyweight?
It may introduce runtime costs for passing or computing extrinsic state and finding flyweights.
What is the key exam distinction in Flyweight?
Separate shared intrinsic state from unshared extrinsic state.
What is a good memory trick for Flyweight?
Share what stays the same, pass in what changes.