In my understanding the purpose of the flyweight pattern is to decrease memory footprint and increase performance by sharing common, extrinsic state. Why would anyone prefer to implement the pattern over storing the shared state in static fields?
Consider the following example: http://www.oodesign.com/flyweight-pattern-wargame-example-java-sourcecode.html
If I am right then the point in this example is to share the common state (soldierGraphicalRepresentation object) between all instances of the SoldierClient class by holding a reference to a single SoldierImp object.
Why would I hassle with implementing this design? I would be tempted to declare the SoldierClient class as follows:
public class SoldierClient implements Soldier
{
protected static Object soldierGraphicalRepresentation;
private int currentLocationX;
private int currentLocationY;
static SoldierImp()
{
soldierGraphicalRepresentation = LoadGraphicalRepresentation();
}
public void moveSoldier(int previousLocationX, int previousLocationY, int newLocationX, int newLocationY) {
// do stuff with the graphical representation
}
}
This way all instances of the SoilderClient share a reference to the same soldierGraphicalRepresentation object and the same goal is achieved. Am I wrong?