Detailed explanation of observer mode of PHP design pattern [StarCraft game case]

Time:2021-3-2

This article illustrates the observer pattern of PHP design pattern. The details are as follows:

When we open a map in the interstellar and fight with several computers, several computer players are equivalent to an alliance. Once we attack a computer, the rest of the computers will send troops to rescue.

So how to let each computer know that its allies have been attacked? And automatically respond?

Problems to be solved:Once a computer is attacked by us, other computers will know and send troops to rescue automatically.

Thinking:Set up some extra observation system for computers, and let them inform other computers.

Example of observer mode:

<?php
  //Abstract alliance class
  abstract class abstractAlly 
  {
    //Place a collection of observers, here is a simple array to visually demonstrate
    public $oberserverCollection;

    //How to add an observer. The parameter is the name of the observer (also the player)
    public function addOberserver($oberserverName)
    {
      //Put the observer object into the collection of observers in the form of elements
      $this->oberserverCollection[] = new oberserver($oberserverName);
    }

    //Inform the observers of the name of the attacked computer
    public function notify($beAttackedPlayerName)
    {
      //Put the observer's collective circle
      foreach ($this->oberserverCollection as $oberserver)
      {
        //Call the rescue function of each observer. The parameter is the name of the attacked computer. If is used to exclude the observer of the attacked computer
        if($oberserver->name != $beAttackedPlayerName) 
        {
          $oberserver->help($beAttackedPlayerName);  
        }
      }
    }

    abstract public function beAttacked($beAttackedPlayer);
  }

  //Specific alliances
  class Ally extends abstractAlly 
  {
    //Constructor, takes an array of the names of all computer players as parameters
    public function __construct($allPlayerName)
    {
      //Loop the array of all computer players
      foreach ($allPlayerName as $playerName)
      {
        //Add observer, and the parameter is the name of each computer player
        $this->addOberserver($playerName);
      }
    }

    //Inform the observers of the name of the attacked computer
    public function beAttacked($beAttackedPlayerName)
    {
      //Call the rescue function of each observer. The parameter is the name of the attacked computer. If is used to exclude the observer of the attacked computer
      $this->notify($beAttackedPlayerName);
    }
  }

  //Observer interface
  interface Ioberserver 
  {
    //Defining and standardizing rescue methods
    function help($beAttackedPlayer);
  }

  //Specific observer classes
  class oberserver implements Ioberserver 
  {
    //The name of the observer (and player) object
    public $name;

    //Constructor, the parameter is the name of the observer (also the player)
    public function __construct($name)
    {
      $this->name = $name;
    }

    //Methods of rescue by observers
    public help($beAttackedPlayerName)
    {
      //Here simple output, who to save who, and finally add a new line, easy to display
      echo $this->name." help ".$beAttackedPlayerName."<br>";
    }

    abstract public function beAttacked($beAttackedPlayer);
  }

  //Suppose I'm one on three, two Zerg, one Protoss
  $allComputePlayer = array('Zerg1', 'Protoss2', 'Zerg2');

  //New computer Alliance
  $Ally = new Ally($allComputePlayer);

  //Suppose I attack the second Zerg
  $Ally->beAttacked('Zerg2');

?>

Usage Summary:Observer mode can immediately notify all related objects of a state change, and call each other’s processing methods.

Implementation Summary:An observer class is needed to handle changes, and the observed object needs to implement a method to notify all observers.

More about PHP related content, interested readers can see the special topics of this website: “PHP object-oriented programming introductory tutorial”, “PHP array operation skills encyclopedia”, “PHP basic grammar introductory tutorial”, “PHP operation and operator Usage Summary”, “PHP string Usage Summary”, “PHP + MySQL database operation introductory tutorial” and “PHP common database operation” Summary of writing skills

I hope this article is helpful for PHP programming.