Pregunta

I have a matrix (bidimensional array) of Objects Bee, I want to apply the flyweight pattern, how should I apply it?, if I instantiate the matrix directly with a huge size I would be allocating too much memory. Is there a efficient way to apply the pattern?.

I already checked that the pattern is applicable to the Bee object, but I'm concerned about the matrix memory. PD I'm using java.

¿Fue útil?

Solución

This is straight from Wikipedia

"Flyweight is a software design pattern. A flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory."

Please see that the pattern suggest reusing the object in case of repeated representation. So first you need to see how many Bee objects in your matrix might be having duplicate objects and hence match and reuse the first object instead of creating the new one in memory.

Create a Factory that would look within the pool of already created objects. If a match is found , it will return the object from the pool and if not will create a new one in memory and will return it.

There could be other ways to save memory too but then would need to see complete code.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top