views:

253

answers:

3

Can anyone please give me any example of situation in a database-driven application where I should use Flyweight pattern?

How can I know that, I should use flyweight pattern at a point in my application?

I have learned flyweight pattern. But not able to understand an appropriate place in my database-driven business applications to use it.

+1  A: 

[Not a DB guy so this is my best guess]

The real bonus to the flyweight pattern is that you can reuse data if you need to; Another example is word processing where ideally you would have an object per "character" in your document, but that wuld eat up way too much memory so the flyweight memory lets you only store one of each unique value that you need.

A second (and perhaps simplest) way to look at it is like object pooling, only you're pooling on a "per-field" level as opposed to a "per-object" level.

In fact, now that i think about it, it's not unlike using a (comparatively small) chunk of memory in c(++) so store some raw data which you do pointer manipulation to get stuff out of.

[See this wikpedia article].

RCIX
-1 A very important part of Flyweight is that data is being *shared* between many instances - they wouldn't be, using your suggested 'large data structure'. Flyweight is best applied to immutable types, so a User (which sounds a lot like a Domain Entity) is a really bad candidate for Flyweight.
Mark Seemann
I added another simpler example. The key is your objects are merely pointing to data instead of actually holding data, so you can have duplicate values point to the same spot and conserve memory.
RCIX
@mark: you have a good point, i removed that example.
RCIX
+2  A: 

You should apply any pattern when it naturally suggests itself as a solution to a concrete problem - not go looking for places in your application where you can apply a given pattern.

Flyweight's purpose is to address memory issues, so it only makes sense to apply it after you have profiled an application and determined that you have a ton of identical instances.

Colors and Brushes from the Base Class Library come to mind as examples.

Since a very important part of Flyweight is that the shared implementation is immutable, good candidates in a data-driven application would be what Domain-Driven Design refers to as Value Objects - but it only becomes relevant if you have a lot of identical values.

Mark Seemann
Not really sure this is a good example, it feels too "generic" to be helpful to the OP.
RCIX
+2  A: 

Except for a very specialized database application, the Flyweight might be used by your application, but probably not for any class that represents an entity which is persisted in your database. Flyweight is used when there otherwise might be a need for so many instantiations of a class that if you instantiated one every discrete time you needed it performance would suffer. So instead, you instantiate a much smaller number of them and reuse them for each required instance by just changing data values for each use. This would be useful in a situation where you might have to instantiate 1000s of such classes each second, which is generally not the case for entities persisted in a database.

Charles Bretana