What are the differences between Flyweight and Object Pool paths?

It seems to me that the Flyweight and Object Pool models are very similar. Both have pools of properties leased to customers. What are the differences?

+3


source to share


1 answer


They differ in the way they are used.

Merged objects can only be used by one "client" at a time. To do this, the pool object must be retrieved from the pool, then it can be used by the client, and then the client must return the object back to the pool. There can be multiple instances of the same object, up to the maximum capacity of the pool.

In contrast, the Flyweight object is single and can be used by multiple clients at the same time.



As far as concurrent access is concerned, pooled objects can be mutable and usually they don't need to be thread safe as they usually are, only one thread will use a particular instance at a time. Flies should be immutable (best option) or thread-safe. (Honestly, I'm not sure if mutating flies are still flies :))

In terms of performance and scalability, pools can become bottlenecks if all of the pooled objects are in use and there are more clients for them, threads will be blocked waiting for an available object from the pool. This does not apply to Flyweight.

+11


source







All Articles