Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flyweight vs object pool patterns: When is each useful?

As far as I know the object pool is a creational pattern and the flyweight is a structural pattern, but actually I can´t see very much difference between the two. Could someone please explain to me the difference and when each could be useful in an implementation?

like image 292
edgar9000 Avatar asked Feb 17 '12 02:02

edgar9000


People also ask

When would you use the flyweight pattern?

Flyweight pattern is used when we need to create a large number of similar objects (say 105). One important feature of flyweight objects is that they are immutable. This means that they cannot be modified once they have been constructed.

What is the advantage of object pool design pattern?

Advantage of Object Pool design patternIt is most effective in a situation where the rate of initializing a class instance is high. It manages the connections and provides a way to reuse and share them. It can also provide the limit for the maximum number of objects that can be created.

What is the main advantages of flyweight design pattern?

Advantages of Flyweight Design Pattern The Flyweight Pattern contributes to improving the performance of the application by reducing the number of objects. The Flyweight Pattern reduces the memory footprint and saving RAM as the common properties are shared between objects using Intrinsic properties.

In which scenario you will prefer the flyweight design pattern most?

The flyweight pattern is best suited to scenarios where the number of objects that the factory will need to create and remember is finite and compatible with the memory constraints on the application.


3 Answers

One difference in that flyweights are commonly immutable instances, while resources acquired from the pool usually are mutable.

So you create flyweights to avoid the cost of repeatedly create multiple instances of objects containing the same state (because they are all the same, you just create only one and reuse it throughout all places in your app), while resources in a pool are particular resources that you want to control individually and possibly have different state, but you don't want to pay the cost of creation and destruction because they are all initialized in the same state.

like image 119
Gabriel Belingueres Avatar answered Nov 13 '22 22:11

Gabriel Belingueres


At least two major differences come to mind:

  • An object pool is a container for a set of domain objects while a flyweight usually is a domain object.
  • An object pool usually contains a set of similar objects that can be shared concurrently, such as database connections, while there is usually a set of different flyweight objects, each representing a different state.
like image 41
casablanca Avatar answered Nov 14 '22 00:11

casablanca


This site describes both patterns with specific examples. It does a pretty goo job clarifying the difference and supports Gabriel's response above. http://www.oodesign.com/

like image 22
slippereddog Avatar answered Nov 13 '22 22:11

slippereddog