2017-09-12 8 views
0

私は、ビヘイビアを適用する抽象的なサブジェクトの実装に基づいて関数の動的な動作を可能にするデザインパターンを探しています。 ステートとコマンドのデザイナーパターンハイブリッド?

抽象例:

interface IPlace 
    {  
    } 

    class Garage implements IPlace 
    { 
    } 

    class Backyard implements IPlace 
    { 
    } 

    class Mover 
    { 
     void Move(IPlace source, IPlace destination); 
    } 

使用例:

void NotMain() 
    { 
     IPlace garage = new Garage(); 
     IPlace backyard = new Backyard(); 
     Mover mover = new Mover(); 
     mover.move(garage, garage); 
     mover.move(garage, backyard); 
     mover.move(backyard, garage); 
     mover.move(backyard, backyard); 
    } 

期待される結果:私は答えはStrategyパターンかもしれないと思うが、私は不明だ

Mover moved an item inside the garage 
Mover moved an item from the garage to the backyard 
Mover moved an item from the backyard to the garage because it's raining 
Mover moved an item around in the backyard 

n 2個の異なる振る舞いがあるので振る舞いをどのように決定すべきか、nは実装の数IPlaceインターフェースおそらくMoveOperationオブジェクトを返すファクトリですか?

私は、この種の問題に対するベストプラクティスの解決に興味があります。

答えて

0

これは、戦略設計パターンのケースではないようです。これは単純な継承です。インターフェイス名を 'I'(IPlace)で開始すると、インプリメンテーションの他の「タイプ」と同じように目的を果たせません。我々は状態に変化がない と

  • ものは交換可能にしたいアルゴリズム
  • の家族を持っているとき

    1. Strategyパターンが適用される

    上記のすべての問題があります。また、PlaceはInterfaceの代わりにAbstractクラスにすることができるので、そこのすべての場所に関連する共通の状態をプッシュすることができます。

  • 関連する問題