18.スタークラフトのphp設計モード--観察者モード

13254 ワード

題記==================================================================================================================================================================================
本論文の住所:http://www.cnblogs.com/davidhhuan/p/4248205.html
=================================================================================================================================
私達が星の間で地図をつけていくつかのコンピュータと戦う時、コンピュータのいくつかのプレーヤーは同盟を結ぶことに相当して、いったん私達が出兵してあるコンピュータに攻撃したら、残りのコンピュータは軍隊を出て救援します。どのようにして各コンピュータに自分の盟友が攻撃されたかを知らせますか?そして自動的に反応しますか?
解決すべき問題:あるコンピュータが攻撃されたら、他のコンピュータが知られて、自動的に軍隊から出動して救援します。
コンピュータのためにいくつかの追加の観測システムを設置し、彼らが他のコンピュータに通知します。
観察者(Observer)モード例:
<?php
    //      
    abstract class abstractAlly 
    {
        //
        public $oberserverCollection;

        //        ,      (    )   
        public function addOberserver($oberserverName)
        {
            //                    
            $this->oberserverCollection[] = new oberserver($oberserverName);
        }

        //                 
        public function notify($beAttackedPlayerName)
        {
            //         
            foreach ($this->oberserverCollection as $oberserver)
            {
                //            ,            ,if              
                if($oberserver->name != $beAttackedPlayerName) 
                {
                    $oberserver->help($beAttackedPlayerName);    
                }
            }
        }

        abstract public function beAttacked($beAttackedPlayer);
    }

    //      
    class Ally extends abstractAlly 
    {
        //
        public function __construct($allPlayerName)
        {
            //            
            foreach ($allPlayerName as $playerName)
            {
                //
                $this->addOberserver($playerName);
            }
        }

        //                 
        public function beAttacked($beAttackedPlayerName)
        {
            //            ,            ,if              
            $this->notify($beAttackedPlayerName);
        }
    }

    //      
    interface Ioberserver 
    {
        //        
        function help($beAttackedPlayer);
    }

    //       
    class oberserver implements Ioberserver 
    {
        //   (    )     
        public $name;

        //    ,      (    )   
        public function __construct($name)
        {
            $this->name = $name;
        }

        //          
        public help($beAttackedPlayerName)
        {
            //       ,    ,       ,    
            echo $this->name." help ".$beAttackedPlayerName."<br>";
        }

        abstract public function beAttacked($beAttackedPlayer);
    }

    //      ,    ,    
    $allComputePlayer = array('Zerg1', 'Protoss2', 'Zerg2');

    //      
    $Ally = new Ally($allComputePlayer);

    //           
    $Ally->beAttacked('Zerg2');

?>
 
用途まとめ:観察者モードはある状態の変化を直ちにすべての関連対象に通知し、相手の処理方法を呼び出すことができます。
総括を実現するためには、変化を扱う観察者クラスが必要であり、観察された対象は、観察者全員に通知する方法を実現する必要がある。
 
関連記事:
 
1.スタークラフトのphp対象(一)
2.スタークラフトのphp対象(二)
3.スタークラフトのphp設計モード--簡易工場モード
4.スタークラフトのphp設計モード--工場方法モード
5.スタークラフトのphp設計モード--抽象工場モード
6.スタークラフトのphp設計モード--ビルダーモード
7.スタークラフトのphp設計モード--仲介者モード
8.スタークラフトのphp設計モード--享元モード
9.スタークラフトのphp設計モード--代理モード
10.スタークラフトのphp設計モード--原型モード
11.スタークラフトのphp設計モード--ディップモード
12.スタークラフトのphp設計モード--テンプレートモード
13.スタークラフトのphp設計モード--正面モード
14.スタークラフトのphp設計モード--状態モード
15.スタークラフトのphp設計モード--策略モード
16.スタークラフトのphp設計モード--コンビネーションモード
17.スタークラフトのphp設計モード--職責チェーンモード
18.スタークラフトのphp設計モード--観察者モード
19.スタークラフトのphp設計モード--ディエゼルモード
20.スタークラフトのphp設計モード--アダプタモード