Thursday, May 17, 2012

Structural Patterns - 6.FlyWeight Pattern

Have you ever imagined that you are standing in between a huge array of army in the battlefield? The army may consists of horses,elephants,chariots and big mass of soldiers.
Seeing such a scene itself brings a mammoth outlook before your eyes.


How about drafting a design to accomodate the army?

An army may contain thousands/lakhs of soldiers. Think about creating an object for each and every soldier. We have elephants,horses,chariots and soldiers on foot assembled in a disciplined structure with their positions.

How about designing an order of positions for this army?

At first sight, it really looks like a costly effort. Because, we will end up in creating lakhs/thousands of objects to represent the army.

A solution to this type of problems could be a Flyweight Pattern. It helps us to minimize the number of objects created by generalizing the common pattern observed
among them.


This pattern is a collaboration of Factory Method and Singleton design patterns.

Now, let us think a way to simplify the object structure by recognizing the patterns from the army. We can identify some classifications among the soldiers like elephants, horses, chariots and normal soldiers. But, we need to maintain position for each of them.

First, we generalize the soldiers based on their nature, which could be the intrinsic property.So, we will end up in creating just four objects(chariots, elephants, horses, soldiers).

Then, the position to which they need to occupy ,could be more dynamic and explicitly specified.

For example, let us consider we have 1000 soliders in an army.

It consists of 100 chariots, 200 Elephants, 200 Horses, 500 Soldiers and they were positioned in rows 4,3,2 and 1 respectively.

We construct 4 objects for the types of soldiers in an army and we dynamically initialize their position in the battlefield.

Following design explains this approach in a better way,



Soldier is the Flyweight object constructed using the SoldierFactory which is a Flyweight Factory. This flyweight object is a singleton for each and every soldier type.

After receiving the flyweight object client can decide where to position each and every soldier.

FlyweightFactory Code snippet,

private static HashMap<String,Soldier> soldiers=new HashMap<String,Soldier>();
 public static Soldier getSoldier(String type){
  Soldier soldier=soldiers.get(type);
  if(soldier==null){
   soldier=new Soldier(type);
   soldiers.put(type, soldier);
  }
  return soldier;
 }


Client code,

   Soldier soldier;
   for(int i=1;i<=1000;i++){
   if(i<=100){
    soldier=SoldierFactory.getSoldier("Chariot");
    soldier.setPosition(4);
   }
   else if(i<=300){
    soldier=SoldierFactory.getSoldier("Elephant");
    soldier.setPosition(3);
   }
   else if(i<=500){
    soldier=SoldierFactory.getSoldier("Horse");
    soldier.setPosition(2);
   }
   else{
    soldier=SoldierFactory.getSoldier("Soldier");
    soldier.setPosition(1);
   }  


This will result in creating just 4 objects and configure them 1000 times with their position.

No comments:

Post a Comment