Factory method analysis of PHP design pattern [StarCraft game case]

Time:2021-8-29

This paper illustrates the factory method pattern of PHP design pattern. Share with you for your reference, as follows:

The factory mode mentioned in the PHP manual is actually a simple factory mode. Here we discuss an extension of the simple factory pattern: the factory method pattern.

Problems to be solved: Although the simple factory solves the problem of dynamically returning different types of objects, in practice, it often needs to do some additional processing when creating a new object. For example, when manufacturing a machine gunner, you need to judge whether the crystal mine is greater than 50, and when manufacturing a fireman, you need to judge whether the crystal mine is greater than 50 and the gas mine is greater than 25 at the same time, And whether a research institute has been built. If you put all these codes into the factory manufacturing class, the manufacturing class will be very bloated. Moreover, with more and more types of objects produced by the factory, the factory manufacturing class code will be more and more difficult to maintain.

thinking: the factory class (class of arms manufacturer) in the simple factory mode remains unchanged. Add a manufacturing interface, define a method of actual manufacturing object, and then define each factory manufacturing different objects. At the same time, these factories are required to implement this manufacturing interface and let these factories implement the method of actual manufacturing object.

Example of factory method mode:

We put the code of the machine gunner class and the class that makes the machine gunner into a file, marine.php, and its code is as follows:

<?php
  //Machine gunners
  class Marine
  {
    //Methods of attack by machine gunners
    public function attack()
    {
      echo 'Marine attack';
    }
  }
  //The class that manufactures the machine gunner and executes the interface abstractcreator
  class MarineCreator implements abstractCreator
  {
    //Method of actually manufacturing machine gunners
    public function realCreate()
    {
      //If the crystal ore is greater than 50, this is only for illustration, because the ore variable does not exist, and the treatment of crystal less than 50 is not considered
      if($ore>50)
      {
        return new Marine();
      }
    }
  }
?>

We put the code of the fireman class and the class that makes the fireman into a file, firebat.php. Its code is as follows:

<?php
  //Firemen
  class Firebat
  {
    //Method of fireman attack
    public function attack()
    {
      echo 'Firebat attack';
    }
  }
  //Create the class of fireman and execute the interface abstractcreator
  class FirebatCreator implements abstractCreator
  {
    //Method of actually manufacturing firemen
    public function realCreate()
    {
      //If the crystal ore is greater than 50 and the gas ore is greater than 25, and the research institute already exists. This is just for illustration, because there are no ore, gas and academy variables, and the processing when resources are insufficient is not considered
      if($ore>50 && $gas>25 && Academy>1)
      {
        return new Firebat();
      }
    }
  }
?>

The contents in the main file are as follows:

<?php
  //Interfaces that must be implemented by each specific plant
  interface abstractCreator
  {
    //Specify the methods to be implemented by each specific plant
    public function realCreate();
  }
  //The category of arms manufacturers, that is, the main factory
  class BarracksCreator
  {
    //Methods of manufacturing arms
    public create($createWhat)
    {
      //Dynamically load the required class definition file according to the input parameters
      require_once($createWhat.'.php');
      //According to the input parameters, dynamically obtain the class name of the corresponding specific factory
      $creatorClassName = $createWhat.'Creator';
      //Create a specific factory object
      $creator = new $creatorClassName;
      //Use the specific factory to actually produce, and then return the object of the required class. Because they all execute the interface abstractcreator, they must implement the method realcreate()
      return $creator->realCreate();
    }
  }
  //Create a new arms maker object
  $creator = new BarracksCreator();
  //Create a fireman object by receiving parameters
  $troop1 = $creator->create('Marine');
  $troop1->attack();
  //Create a machine gunner object by receiving parameters
  $troop2 = $creator->create('Firebat');
  $troop2->attack();
?>

Purpose summary:The factory method mode assigns the task of creating new objects to the corresponding specific factory class. There is no need to modify the external main factory because some production objects need additional processing.

Implementation Summary:The main factory class that needs to receive parameters, such as the arms manufacturer barrackscreator above, also needs to declare an interface for specific manufacturing methods, such as abstractcreator above, and then define specific factory classes for specific production of various products. Each specific factory class must execute interface abstractcreator, so that they must implement the methods of manufacturing objects, For example, realcreate() above. When using, you only need to pass the parameters to the production method create() of the main factory class factory, and then create() generates the object of the specific factory class according to the parameters, and calls the specific factory class realcreate() to obtain the manufactured product object and return it. For external use, you only need to call the main factory class factory for production.
Note: in fact, the factory method mode in this article is different from that in some articles. The standard factory mode often uses an abstract class to replace the interface abstractcreator above, and then allows all specific factory classes to inherit it. However, when using, because the abstract class cannot be instantiated (create its object), it is often new firebatcreator () directly in the code, However, the simple factory pattern can solve the problem of direct new, so I use the simple factory pattern and the factory method pattern together to make the example here more practical. At the same time, because PHP is single inheritance and there is no limit to the number of execution interfaces, it is more flexible to use the interface abstractcreator.

For more information about PHP, readers who are interested can see the topics on this site: introduction to PHP object-oriented programming, complete operation skills of PHP array, introduction to PHP basic syntax, summary of PHP operation and operator usage, summary of PHP character string usage Introduction to PHP + MySQL database operation and summary of common PHP database operation skills

I hope this article will help you in PHP programming.