7
There’s something about Desing Pattern Flyweight that’s really getting me confused. Briefly, the pattern says that to save memory in a situation where you use several similar objects, you use a factory that returns references to the same object in a flyweights pool instead of instantiating thousands of different objects.
I also saw that to generate data independence between each object, it is necessary to understand two concepts in this pattern: Extrinsic and intrinsic data, of which I also did not understand very well.
If using this pattern I am using references to the same object, won’t you have problems changing the state of only one object and end up changing the state of all? For example: I developed a game in which, using this pattern, I own the port object that is shared between all the "ports" of the game; the player controls a character that can open or close doors. If all ports in the game are referencing the same object, the game will not have the bug of when the character opens a door, inexplicably all ports in the same scenario open?
I saw that this problem could be solved with the concept of extrinsic and intrinsic data, but in all the examples I read, I didn’t understand how this is implemented. How, actually is implemented a state of state independence between several flyweights?
I love the example that involves textures of objects to explain the Fly Weight :)
– Oralista de Sistemas