格静的インスタンスの非staticのものにパターンフライ級
-
22-10-2019 - |
質問
また、フライ級のサンプルコードで http://www.oodesign.com/flyweight-pattern-wargame-example-java-sourcecode.html 迷ったらかった場合に割り当て静的インスタンス(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
}
解決
a SoldierClient
保持しません コピー の SOLDIER
, 、それはaを保持します 参照 に SOLDIER
, 、 と 毎日 SoldierClient
への参照を保持します 同じ SOLDIER
.
編集に答える
各兵士の場所は開催されます SoldierClient
インスタンス(currentLocationX
と currentLocationY
プロパティ)。これらのプロパティのコメントコメントは、「この状態はクライアントによって維持されている」(つまり、「この状態はで維持されていない」と語っています。 SoldierImp
実例")。
すべてが入っています moveSoldier
'のパラメーター:noはありません SoldierImp
インスタンス状態。静的ユーティリティ方法のように考えてください。座標はによって提供されます SoldierClient
実例;彼らは決して保存されません SoldierImp
- 使用されています。
他のヒント
コンピュて記述:
このセクションの共通の状態の兵士の共有 オブジェクト
本当に毎SolderClientへの参照が兵のないコピーです。毎SolderClientの変数 兵隊の兵士 はreferencigだ一つのオブジェクトantで同じすべております。
以来、フライ級のパターンを採用しシングルトンパターンも確認できますし初
各兵士clientインスタンスにはを持っています 参照 兵士に。この場合、それらはすべて同じインスタンスを指しています。 SoldierFactoryへの呼び出しごとに、同じ兵士オブジェクトが返されることに気付くでしょう。コールソルジャーのコンストラクターは1つだけです。
参照してください シングルトン