Reference in Flyweight design pattern

Asked

Viewed 358 times

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?

2 answers

5

I don’t know if these terms "intrinsic" and "extrinsic" have a t or if they are only part of the explanation of the chapter but there is nothing magical about it. At the end of the day, if you want to use the optimization described by flyweight, shared objects need to be immutable or you’ll find these problems you’re imagining. If you need to have some mutable property that is not to be shared it needs to be stored in a separate object than the shared part.

For example, suppose your doors have two properties: the image used to represent it and the opening state (open/closed). The state will have to be an instance variable stored separately by each port object but the images are immutable and can be cached and shared, as occurs in flyweight pattern.

  • I love the example that involves textures of objects to explain the Fly Weight :)

0

Intrinsic data are those that are part of the essence of something. The heart is something intrinsic to the human being.

The extrinsic data are those that vary. The skin tone is something extrinsic of the human being.

Usually you use the Flyweight in things that do not have complex behavior but end up consuming a lot of memory, such as textures.

Yes changing "one object" causes side effects on "others". In your example an option would be to Porta be able to control their own behavior in this way the "other" objects will not be affected.

You can consider the design standard Caching and Object Pool as an alternative to Flyweight or implement in the same Balking or State.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.