Flyweight pattern is one of the structural design patterns as this pattern provides ways to decrease object count thus improving application required objects structure. 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.
Why do we care for number of objects in our program?
In Flyweight pattern we use a HashMap that stores reference to the object which have already been created, every object is associated with a key. Now when a client wants to create an object, he simply has to pass a key associated with it and if the object has already been created we simply get the reference to that object else it creates a new object and then returns it reference to the client.
Intrinsic and Extrinsic States
To understand Intrinsic and Extrinsic state, let us consider an example.
Suppose in a text editor when we enter a character, an object of Character class is created, the attributes of the Character class are {name, font, size}. We do not need to create an object every time client enters a character since letter ‘B’ is no different from another ‘B’ . If client again types a ‘B’ we simply return the object which we have already created before. Now all these are intrinsic states (name, font, size), since they can be shared among the different objects as they are similar to each other.
Now we add to more attributes to the Character class, they are row and column. They specify the position of a character in the document. Now these attributes will not be similar even for same characters, since no two characters will have the same position in a document, these states are termed as extrinsic states, and they can’t be shared among objects.
Implementation : We implement the creation of Terrorists and Counter Terrorists In the game of Counter Strike. So we have 2 classes one for Terrorist(T) and other for Counter Terrorist(CT). Whenever a player asks for a weapon we assign him the asked weapon. In the mission, terrorist’s task is to plant a bomb while the counter terrorists have to diffuse the bomb.
Why to use Flyweight Design Pattern in this example? Here we use the Fly Weight design pattern, since here we need to reduce the object count for players. Now we have n number of players playing CS 1.6, if we do not follow the Fly Weight Design Pattern then we will have to create n number of objects, one for each player. But now we will only have to create 2 objects one for terrorists and other for counter terrorists, we will reuse then again and again whenever required.
Intrinsic State : Here ‘task’ is an intrinsic state for both types of players, since this is always same for T’s/CT’s. We can have some other states like their color or any other properties which are similar for all the Terrorists/Counter Terrorists in their respective Terrorists/Counter Terrorists class.
Extrinsic State : Weapon is an extrinsic state since each player can carry any weapon of his/her choice. Weapon need to be passed as a parameter by the client itself.
Class Diagram :
import java.util.Random;
import java.util.HashMap;
interface Player
{
public void assignWeapon(String weapon);
public void mission();
}
class Terrorist implements Player
{
private final String TASK;
private String weapon;
public Terrorist()
{
TASK = "PLANT A BOMB" ;
}
public void assignWeapon(String weapon)
{
this .weapon = weapon;
}
public void mission()
{
System.out.println( "Terrorist with weapon "
+ weapon + "|" + " Task is " + TASK);
}
}
class CounterTerrorist implements Player
{
private final String TASK;
private String weapon;
public CounterTerrorist()
{
TASK = "DIFFUSE BOMB" ;
}
public void assignWeapon(String weapon)
{
this .weapon = weapon;
}
public void mission()
{
System.out.println( "Counter Terrorist with weapon "
+ weapon + "|" + " Task is " + TASK);
}
}
class PlayerFactory
{
private static HashMap <String, Player> hm =
new HashMap<String, Player>();
public static Player getPlayer(String type)
{
Player p = null ;
if (hm.containsKey(type))
p = hm.get(type);
else
{
switch (type)
{
case "Terrorist" :
System.out.println( "Terrorist Created" );
p = new Terrorist();
break ;
case "CounterTerrorist" :
System.out.println( "Counter Terrorist Created" );
p = new CounterTerrorist();
break ;
default :
System.out.println( "Unreachable code!" );
}
hm.put(type, p);
}
return p;
}
}
public class CounterStrike
{
private static String[] playerType =
{ "Terrorist" , "CounterTerrorist" };
private static String[] weapons =
{ "AK-47" , "Maverick" , "Gut Knife" , "Desert Eagle" };
public static void main(String args[])
{
for ( int i = 0 ; i < 10 ; i++)
{
Player p = PlayerFactory.getPlayer(getRandPlayerType());
p.assignWeapon(getRandWeapon());
p.mission();
}
}
public static String getRandPlayerType()
{
Random r = new Random();
int randInt = r.nextInt(playerType.length);
return playerType[randInt];
}
public static String getRandWeapon()
{
Random r = new Random();
int randInt = r.nextInt(weapons.length);
return weapons[randInt];
}
}
|
Output:
Counter Terrorist Created
Counter Terrorist with weapon Gut Knife| Task is DIFFUSE BOMB
Counter Terrorist with weapon Desert Eagle| Task is DIFFUSE BOMB
Terrorist Created
Terrorist with weapon AK-47| Task is PLANT A BOMB
Terrorist with weapon Gut Knife| Task is PLANT A BOMB
Terrorist with weapon Gut Knife| Task is PLANT A BOMB
Terrorist with weapon Desert Eagle| Task is PLANT A BOMB
Terrorist with weapon AK-47| Task is PLANT A BOMB
Counter Terrorist with weapon Desert Eagle| Task is DIFFUSE BOMB
Counter Terrorist with weapon Gut Knife| Task is DIFFUSE BOMB
Counter Terrorist with weapon Desert Eagle| Task is DIFFUSE BOMB
Further Read – Flyweight Method in Python
References:
This article is contributed by Chirag Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above