我正在浏览http://www.oodesign.com/flyweight-pattern-wargame-example-java-sourcecode.html上的 Flyweight 示例代码,并想知道当我们分配静态实例时它是如何工作的(SOLDIER
如上面的网站) 到一个非静态士兵实例中SoldierClient
,我们是否真的减小了对象大小,因为每个对象SoldierClient
都会以某种方式在我们创建SOLDIER
的每个对象中保存一个实例副本?SoldierClient
编辑:
在moveSoldier()
它说的方法中
// 从先前位置删除士兵表示
// 然后在新位置渲染士兵表示
为什么这不会影响类中创建的所有对象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
}