Note that there are some explanatory texts on larger screens.

plurals
  1. POFlyweight pattern vs static fields
    primarykey
    data
    text
    <p>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?</p> <p>Consider the following example: <a href="http://www.oodesign.com/flyweight-pattern-wargame-example-java-sourcecode.html" rel="nofollow noreferrer">http://www.oodesign.com/flyweight-pattern-wargame-example-java-sourcecode.html</a></p> <p><img src="https://i.stack.imgur.com/lBDZe.png" alt="enter image description here"></p> <p>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.</p> <p>Why would I hassle with implementing this design? I would be tempted to declare the SoldierClient class as follows:</p> <pre class="lang-java prettyprint-override"><code>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 } } </code></pre> <p>This way all instances of the SoilderClient share a reference to the same soldierGraphicalRepresentation object and the same goal is achieved. Am I wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload