Assigning static instances to non-static ones in Flyweight pattern

253 views Asked by At

I was going through the Flyweight sample code at http://www.oodesign.com/flyweight-pattern-wargame-example-java-sourcecode.html and wondering how it really works when we assign a static instance (SOLDIER as in the above site) to a non-static soldier instance in SoldierClient do we really reduce the object size since each SoldierClient will somehow hold a copy of SOLDIER instance in each SoldierClient object we create?

EDIT:

In the method moveSoldier() it says

// delete soldier representation from previous location
// then render soldier representation in new location

How come this doesn't affect all the objects created in class WarGame

package flyweight;

public class SoldierImp implements Soldier {

    /**
     * Intrinsic State maintained by flyweight implementation
     * Solider Shape ( graphical represetation)
     * how to display the soldier is up to the flyweight implementation
     */
    private Object soldierGraphicalRepresentation;

    /**
     * Note that this method accepts soldier location 
     * Soldier Location is Extrinsic and no reference to previous location 
     * or new location is maintained inside the flyweight implementation
     */
    public void moveSoldier(int previousLocationX, int previousLocationY,
            int newLocationX, int newLocationY) {

        // delete soldier representation from previous location 
        // then render soldier representation in new location   
    }
3

There are 3 answers

4
Dave Newton On BEST ANSWER

A SoldierClient doesn't hold a copy of SOLDIER, it holds a reference to SOLDIER, and every SoldierClient holds a reference to the same SOLDIER.

Answering the edit

Each soldier's location is held in SoldierClient instances (currentLocationX and currentLocationY properties). The code comments for those properties spell it out as well: "this state is maintained by the client" (i.e., "this state is not maintained in the SoldierImp instance").

Everything is in moveSoldier's parameters: there's no SoldierImp instance state. Think of it like a static utility method. Coordinates are provided by the SoldierClient instance; they're never stored by the SoldierImp--just used.

0
Ernesto Campohermoso On

As the documentation mentions:

The solution is to keep the common state of soldiers in a shared object

Really every SolderClient has a reference to SOLDIER not a copy. In every SolderClient the variable Soldier soldier is referencig just to one object ant it is the same for all clients.

Since Flyweight pattern uses a Singleton pattern maybe you can check it first:

http://www.oodesign.com/singleton-pattern.html

0
Ray Myers On

Each SoldierClient instance has a reference to a Soldier object. In this case they all point to the same instance. You'll notice that for each call to SoldierFactory, the same Soldier object is returned -- there is only one call Soldier's constructor.

See also Singleton