17

戦略パターンが提供する利点について理解できないようです。以下の例を参照してください。

//Implementation without the strategy pattern
class Registry {

    public function Func1(){
         echo 'called function 1';
    }

    public function Func2(){
         echo 'called function 2';
    }

}

$client = new Registry();
$client->Func1();
$client->Func2();

//Implementation with strategy pattern
interface registry {
     public function printMsg();
}

class Func1 implements registry {
     public function printMsg(){
         echo 'called function 1';
    }
}

class Func2 implements registry {
     public function printMsg(){
         echo 'called function 2';
    }
}

class context {

      public function printMsg(Registry $class){
          $class->printMsg();
      }
}

$client = new context();
$client->printMsg(new Func1());
$client->printMsg(new Func2());

上記の 2 つの例で、戦略パターンにはどのような利点があり、最初のアプローチよりもどのように優れていますか? なぜ戦略パターンを使用する必要があるのですか?

上記のコード例にはエラーが含まれている可能性がありますが、コードは無視してください。

4

6 に答える 6

9

基本的に、戦略は複数のクラスにまたがる機能をグループ化するためのものです。ああ、コードにタイプミスがあります

class context {

      public function printMsg(registry $class){
          $class->printMsg();
      }
}

タイプヒンティングのインターフェースの名前で。より明確にするために、小さな例を示しましょう。あなたがiPhoneとAndroidを持っていると想像してみてください。それらの共通点は何ですか? 1つは、両方とも電話です。このようなインターフェースを作成できます

interface Telephone {
    public function enterNumber();
    public function call();
    public function sentTextMessage();
}

各電話機にインターフェイスを実装します。

class Iphone implements Telephone {
     // implement interface methods
}

class Android implement Telephone {

}

また、車の GPS のようなサービスをすべての電話に接続することもできます。電話を接続する場合は (通常はインターフェース BlueTooth を使用して) 確認してください。次のようなことができます。

class carGps{
   public function handFreeCall(Telephone $tel){
       $this->amplifyVolume($tel->call());
   }
}


$tomtom = new CarGps();
$tomtom->handFreeCall(new Iphone());
//or if you like to:
$tomtom->handFreeCall(new Android());

アプリケーション開発者は、コードを壊すことなく Telephone インターフェースを実装するすべての電話で handFreeCall を使用できます。これは、電話が通話可能であることを知っているからです。

私が助けてくれることを願っています。

于 2013-07-18T11:44:38.383 に答える
2

戦略パターンは、特定の作業を行うためのアルゴリズム的アプローチを抽象化するのに役立ちます。さまざまなアルゴリズムを使用して数値の配列を並べ替えたいとします。この場合、アルゴリズムをコードに密結合するよりも、戦略パターンを適用する方が適切です。

戦略で構成されるクラスをインスタンス化するクラスでは、構成されたクラスのコンストラクターに渡すことにより、戦略クラス参照をインスタンス化します。

このように、実装ではなくインターフェイスにプログラミングしているため、いつでも戦略クラスの階層にクラスを追加して、戦略で構成されるクラスに同じものを渡すことができます

以下のリンクからお進みください

于 2013-07-18T11:26:23.800 に答える
0

多くの場合、例はやや奇妙で、アヒル、猫などを表しています。アラートの表示に使用される戦略パターンの例を次に示します。(ゴードンの答えを拡張する)。

1. さまざまなメソッドのインターフェース (つまり、この場合のアラート形式):

require_once("initialize/initialize.php");
interface alert{
public function alert($message);
};

2. アラート インターフェイスを実装するメソッド。

class alertBorderBullet implements alert{
public function alert($message){
$alert = "<p style='border:1px solid #eee; padding:4px; padding-left:8px; padding-right:8px; border-left:4px solid #FC0; margin-top:8px; margin-bottom:8px; color:#888'>".$message."</p>";
return $alert;
}
};

class alertOrangeBgnd implements alert{
public function alert($message){
$alert = "<p style='color:#fff; background-color:#ff9c3a; padding:4px; padding-left:8px; padding-right:8px; margin-top:8px; margin-bottom:8px; border-left:4px solid #e471bd;'>".$message."</p>";
return $alert;
}
};

class alertRed implements alert{
public function alert($message){
$alert = "<p style='color:#c11; background-color:#efefef; padding:4px; padding-left:12px; padding-right:8px; margin-top:8px; margin-bottom:8px;'>".$message."</p>";
return $alert;
}
};

3. メッセンジャー。アラート メソッドの設定と、プロジェクト内の他のオブジェクトからの取得を分離します。

class alertMessenger{
protected $_alert;
public function setAlertType(alert $alert){$this->_alert = $alert;}
public function returnAlert($message){return $this->_alert->alert($message);}
};

4. さまざまな方法で「アラート」を使用するランダム プロジェクト オブジェクト。

class randomObject{
public $alert;
public function __construct(){
    $this->alert = new alertMessenger;
}
// More code here...
};

$randomObject = new randomObject;
$randomObject->alert->setAlertType(new alertRed);
echo $randomObject->alert->returnAlert($message="Red text for critical info");
$randomObject->alert->setAlertType(new alertBorderBullet);
echo $randomObject->alert->returnAlert($message="Border, bullet and pale-color text");
echo $randomObject->alert->returnAlert($message="Repeat, check style permanence");
$randomObject->alert->setAlertType(new alertOrangeBgnd);
echo $randomObject->alert->returnAlert($message="Again, another redefined message style");

randomObject は、初期化されると (この場合)、自動的に alertMessanger のインスタンスを作成し、そのメソッドを使用可能にします。動作を設定して、メッセージをエコーすることができます。setAlertType と returnAlert によって、必要に応じて他のアラート形式を作成して使用できます。

于 2016-12-26T19:00:18.850 に答える
0

ギャング オブ フォーが想定している戦略パターンの重要な機能は次のとおりです。

  1. 条件文はありません(したがって、switch、if などの条件文は省略してください)
  2. コンテキスト参加者を持っています

戦略に関するアドバイスのほとんどは、コンテキスト参加者を除外しています。ここでは、PHP の Strategy パターンの 5 つの異なる例を見つけることができます: http://www.php5dp.com/category/design-patterns/strategy/

于 2014-07-29T12:08:54.253 に答える
0

ここにライブラリと symfony バンドルがあります:

https://packagist.org/packages/pugx/godfather

于 2013-12-26T15:44:24.070 に答える