| <?php The Abstract Association Class abstractclassabstractally{ Place the collection of observers, here with a simple array to visually demonstrate Public$oberservercollection; Increase the observer's method, the parameter is the Observer (also the player) name Publicfunctionaddoberserver ($oberserverName) { To put an observer object into the viewer's collection in an element manner $this->oberservercollection[]=newoberserver ($oberserverName); } Notify each observer of the name of the attacked computer Publicfunctionnotify ($beAttackedPlayerName) { Loop The Observer's collection foreach ($this->oberservercollectionas$oberserver) { Call each observer's rescue function, which is the name of the attacked computer, if used to exclude the observer of the attacked computer if ($oberserver->name!= $beAttackedPlayerName) $oberserver->help ($beAttackedPlayerName); } } Abstractpublicfunctionbeattacked ($beAttackedPlayer); } Specific Allied Classes classallyextendsabstractally{ Constructor that takes an array of the names of all computer players as arguments Publicfunction__construct ($allPlayerName) { Loop the array of all the computer players foreach ($allPlayerNameas $playername) { Add observer, parameters for each computer player's name $this->addoberserver ($playerName); } } Notify each observer of the name of the attacked computer Publicfunctionbeattacked ($beAttackedPlayerName) { Call each observer's rescue function, which is the name of the attacked computer, if used to exclude the observer of the attacked computer $this->notify ($beAttackedPlayerName); } } The Observer's interface interfaceioberserver{ Define a standard rescue method Functionhelp ($beAttackedPlayer); } The specific observer class classoberserverimplementsioberserver{ The name of the Observer (who is also the player) object Public$name; constructor, parameter is the name of the Observer (also the player) Publicfunction__construct ($name) { $this->name= $name; } The observer's way to rescue the people Publichelp ($beAttackedPlayerName) { Here simple output, who to save who, and finally add a line, easy to display Echo$this->name. " Help ". $beAttackedPlayerName." <br> "; } Abstractpublicfunctionbeattacked ($beAttackedPlayer); } Let's say I'm a couple of three, two Zerg, a Protoss. $allComputePlayer =array (' Zerg1 ', ' Protoss2 ', ' Zerg2 '); New computer Alliances $Ally =newally ($allComputePlayer); Let's say I attacked the second Zerg. $Ally->beattacked (' Zerg2 '); ?> |