18. StarCraft PHP design mode-Observer mode

Source: Internet
Author: User

Preface
==============================================================================
This PHP design mode album from the blog (jymoz.com), now has no access, this series of articles I have been looking for a long time to find the complete, thanks to the author Jymoz's Hard Pay Oh!

This address:http://www.cnblogs.com/davidhhuan/p/4248205.html

==============================================================================


When we open a map in the interstellar and a few computers to fight, the computer's several players equal alliances, once we attack a computer, the rest of the computer will send troops to rescue.
So how do you get computers to know that their allies have been attacked? and automatically react?

problem to be solved: Once a computer is attacked by us, other computers are informed and automatically dispatched to the rescue.

idea: set up some extra observation systems for your computer and let them notify other computers.

Observer (Observer) Pattern Example:

<?PHP//the abstract Alliance class    Abstract classabstractally {//Place The Observer's collection, which is a simple array to visually demonstrate         Public $oberserverCollection; //To increase the observer's method, the parameter is the name of the Observer (also the player)         Public functionAddoberserver ($oberserverName)        {            //puts the Observer object into the collection of observers in an element way            $this->oberservercollection[] =NewOberserver ($oberserverName); }        //notify individual observers of the name of the computer being attacked         Public functionNotify$beAttackedPlayerName)        {            //to cycle the Observer's collection            foreach($this->oberservercollection as $oberserver)            {                //invoke the rescue function of each observer, the name of the computer being attacked, and if it is used to exclude the observer of the attacked Computer                if($oberserver->name! =$beAttackedPlayerName)                 {                    $oberserver->help ($beAttackedPlayerName); }            }        }        Abstract  Public functionBeattacked ($beAttackedPlayer); }    //the specific Alliance class    classAllyextendsabstractally {//constructor that takes an array of the names of all computer players as parameters         Public function__construct ($allPlayerName)        {            //Loop all the computer players ' arrays            foreach($allPlayerName  as $playerName)            {                //add observers, parameters for each computer player's name                $this->addoberserver ($playerName); }        }        //notify individual observers of the name of the computer being attacked         Public functionBeattacked ($beAttackedPlayerName)        {            //invoke the rescue function of each observer, the name of the computer being attacked, and if it is used to exclude the observer of the attacked Computer            $this->notify ($beAttackedPlayerName); }    }    //interface of the viewer    InterfaceIoberserver {//defining a canonical rescue method        functionHelp$beAttackedPlayer); }    //the specific observer class    classOberserverImplementsIoberserver {//the name of the viewer (also the player) object         Public $name; //constructor, parameter is the name of the Observer (also the player)         Public function__construct ($name)        {            $this->name =$name; }        //the method by which the observer carries out the rescue         PublicHelp$beAttackedPlayerName)        {            //here simple output, who to save who, finally add a newline, easy to display            Echo $this->name. "Help".$beAttackedPlayerName." <br> "; }        Abstract  Public functionBeattacked ($beAttackedPlayer); }    //Suppose I'm a pair of three, two Zerg, a Protoss    $allComputePlayer=Array(' Zerg1 ', ' Protoss2 ', ' Zerg2 '); //New computer Alliances    $Ally=NewAlly ($allComputePlayer); //Suppose I attack the second Zerg.    $Ally->beattacked (' Zerg2 ');?>

Usage Summary: The Observer pattern can immediately notify all related objects of a state change and invoke the other's processing method.

Implementation Summary: an observer class is required to handle the change, and the object being observed needs to implement a method that notifies all observers.

Related articles:

1. StarCraft PHP Object-oriented (i)

2. StarCraft PHP Object-oriented (ii)

3. StarCraft PHP design mode-Simple Factory mode

4. StarCraft PHP Design mode-factory method mode

5. StarCraft PHP design mode-Abstract Factory mode

6. StarCraft PHP design mode-builder mode

7. The PHP design mode of StarCraft--The mediator mode

8. StarCraft PHP design mode--Enjoy meta mode

9. StarCraft PHP Design mode--proxy mode

10. StarCraft PHP design mode-prototype mode

11. The PHP design mode of StarCraft--Memo mode

12. StarCraft PHP design mode-template mode

13. StarCraft PHP design mode-positive mode

14. StarCraft PHP design mode-state mode

15. StarCraft PHP design mode--strategy mode

16. StarCraft PHP Design mode--combination mode

17. StarCraft PHP Design mode--responsibility chain mode

18. StarCraft PHP design mode-Observer mode

19. The PHP design mode of StarCraft--iterator mode

? 20. StarCraft PHP design mode-Adapter mode

18. StarCraft PHP design mode-Observer mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.