我正在浏览位于http://www.oodesign.com/flyweight-pattern-wargame-example-java-sourcecode.html的Flyweight示例代码,并且想知道当我们将静态实例(如上述网站中的SOLDIER
)分配给非静态士兵时它是如何工作的在SoldierClient
实例我们是否真的减小了对象大小,因为每个SoldierClient
都会以某种方式持有我们创建的每个SoldierClient
对象中的SOLDIER
实例副本?以享元模式向静态实例分配静态实例
编辑:
在该方法中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
}
+1见,但在这里,我的困惑是,如果我们改变引用的属性,也应该体现在静态对应,但在这种情况下,它不? – coder9
@ coder9您链接的代码永远不会更改引用的属性;实际上参考*有*没有属性。 –
问题编辑如上。谢谢 – coder9