Home > Patterns and Practices > Flyweight Pattern

Flyweight Pattern

Flyweight Pattern

Problem:

You have a large number of objects in the system and how to minimize the memory used by them.

 

Solution:

Pool the objects and share them. An object pool is something like a flyweight. The .NET Framework string interning is an example of a Flyweight variation.  I cannot think of any practical example for a FlyWeight.

The GoF Class structure and the object diagrams are shown below:

Update:

Well this is not enough to understand the pattern, I know. But then this pattern is not a frequently used pattern in Application Design. It is mostly used in low level Systems. Here’s a cooked up example let us see if this one is good enough to understand the pattern.

 

Consider the case of a retail store. Let’s retail store wants to go the WalMart way and RFID enable the products they sell. Most of the current retail systems rely some kind of Product Codes, which are primarily some kind of bar-codes. These Universal Product Codes or UPCs (as we will call it from now) enable tracking of products at the category level. Meaning I can track that I have Men’s wear, Casuals, T-Shirt of Size XL and I have 20 of them. There’s no way to identify these 20 items individually. All 20 will have the same UPC and it is possible to track this at the UPC level only not at the individual item level.

 

With the advent RFID technology we can track individual items of a specific UPC / Product at the item level or we can track each of the 20 shirts. Why would a store a want to track at the individual item level and not at the product level? Well let me cook up a scenario again. Assume there is a RFID scanner at the exit door of the retail store which scans for items which are being taken out. Let us also say that this scan system compares this RFID codes with those that of the items sold today list maintained by the Point of Sale (PoS) system. If this code is not found in the PoS system then the theft alarm goes off.

 

Enough of story. Coming to the domain model of such a system, a store can sell 1000s of some items like processed food etc. All items will have most of the state as same, say a product has the following attributes

BusinessUnit – Mens Wear

Department – Casuals

Stroke – T-Shirt

Size – XL

Colour – Blue

UPC – 78912345

Name – “Polo Neck T-Shirt”

Manufacturer – Nike

 

All these attributes are going to be same for the 100 T-Shirts we have in the store. The only different attribute for each T-Shirt is the RFID code, which is different for each of the individual T-Shirts. Now in a Retail Store the items could be in various locations, each of this location is logically called a Stockpot. Examples of Stockpot include Free (Sale Floor Stuff), Soiled & Damaged, Washing and Ironing, Display, Sold etc. Each stockpot has a collection the product items it has physically. Say, the customer picks the stuff from Free Stock and sales clerk checks it out at the PoS system. The item has to be removed from the Free Stockpot to the SoldItems Stockpot. Let’s say we mark in a database (which is available to the Theft alarm system) that this specific Item with RFID code blah blah blah is sold.

 

This is where the flyweight pattern helps. Since objects (100 T-Shirts) have similar attributes we create it using a FlyWeight factory and share them. Wait a minute the attributes are similar not the same. What about the RFID code? Well that’s what the extrinsic state is all about. The rest of the attributes which remain the same is what is we can call as intrinsic state. Lets say the store also sells some paintings (modern art). It is very unlikely that the store has many items of the same painting. Each painting is an unique product. There is no point in sharing this object with the client. Ah we have a these unique products falling into the category of a UnSharedConcrete flyweight. The generic products which we discussed before (the T-Shirt or any other which you can have multiple items) fall into the category of ConcreteSharedFlyWeights.

 

So much of dry text, now let’s see how this model looks

Another example could be that of an OR Mapper, which puts objects in an Identity Map. Meaning suppose we are loading a collection of order objects and each order object has line item objects which inturn contain product objects. The product objects are pretty much static and don’t vary from order to order. So this objects instead loading another copy of them, the OR Mapper could load and store these objects based on their identity (ProductID) into a hash table and hand over a reference to each of the order line item objects instead of having a new copy for each order. But then you don’t have the concept of intrinsic state and extrinsic state here. Better examples are welcome.
  1. No comments yet.
  1. November 19, 2010 at 11:01 pm

Leave a comment