Explanation of factory mode of PHP design mode of Factory Pattern

  • 2021-12-04 09:35:43
  • OfStack

In object-oriented programming, factory pattern is the most commonly used instantiation object pattern, and factory class is a class specially used to create other objects. Factory class is very important in polymorphic programming practice. It allows dynamic substitution of classes and modification of configuration, which makes applications more flexible. Mastering the factory pattern is essential to Web development, which will give your system greater scalability and minimal modifications.

Factory patterns are often used to return different classes with similar interfaces, and a common use of factories is to create polymorphic providers.

Usually the factory pattern has one key construct, a static method called factory. This static method can accept any number of parameters and must return 1 object.

A very close-to-life example to tell you what the factory model is

But is the factory model really a liability? Actually, it's not! He can be passed down as a design pattern to this day, and there must be a reason for him! However, the examples we see can only explain what the factory pattern is, but can't explain the advantages of the factory pattern very well, so we don't know why and when to use the factory pattern after learning it!

In fact, the factory mode is very common in our real life. Let me give an example in my life, and everyone should be able to understand the use of the factory mode!

Has everyone eaten McDonald's? When we go to order, we can order a hamburger, a coke and a French fries. We can also order a coke and a French fries. After ordering, the orderer will ask us if there is anything else. You said no! Then you have ordered this meal, so you can give money. Gee, we found that this is a builder mode (Builder Pattern)!

(ps: It did happen all of a sudden. I didn't think of this example when I wrote the Builder Pattern article before.)

Basic factory classes:


<?php
 class Fruit {
 //  Object returns from the factory class 
 }
 Class FruitFactory {
 public static function factory() {
  //  Returns the of the object 1 New instances 
  return new Fruit();
 }
 }
 //  Calling factory 
 $instance = FruitFactory::factory();
?>

Use the factory class to produce objects:


<?php
class Example
{
  // The parameterized factory method
  public static function factory($type)
  {
    if (include_once 'Drivers/' . $type . '.php') {
      $classname = 'Driver_' . $type;
      return new $classname;
    } else {
      throw new Exception('Driver not found');
    }
  }
}
// Load a MySQL Driver
$mysql = Example::factory('MySQL');
// Load an SQLite Driver
$sqlite = Example::factory('SQLite');
?>

1 full factory class:

The following program defines a generic factory class that produces an empty object that holds all of your operations. You can get an instance of the operations in that instance.


<?php
  /**
   * Generic Factory class
   * This Factory will remember all operations you perform on it,
   * and apply them to the object it instantiates.
   */
  class FruitFactory {
    private $history, $class, $constructor_args;
    /**
     * Create a factory of given class. Accepts extra arguments to be passed to
     * class constructor.
     */
    function __construct( $class ) {
      $args = func_get_args();
      $this->class = $class;
      $this->constructor_args = array_slice( $args, 1 );
    }
    function __call( $method, $args ) {
      $this->history[] = array(
        'action'  => 'call',
        'method'  => $method,
        'args'  => $args
      );
    }
    function __set( $property, $value ) {
      $this->history[] = array(
        'action'  => 'set',
        'property'  => $property,
        'value'    => $value
      );
    }
    /**
     * Creates an instance and performs all operations that were done on this MagicFactory
     */
    function instance() {
      # use Reflection to create a new instance, using the $args 
      $reflection_object = new ReflectionClass( $this->class ); 
      $object = $reflection_object->newInstanceArgs( $this->constructor_args ); 
      # Alternative method that doesn't use ReflectionClass, but doesn't support variable
      # number of constructor parameters.
      //$object = new $this->class();
      # Repeat all remembered operations, apply to new object.
      foreach( $this->history as $item ) {
        if( $item['action'] == 'call' ) {
          call_user_func_array( array( $object, $item['method'] ), $item['args'] );
        }
        if( $item['action'] == 'set' ) {
          $object->{$item['property']} = $item['value'];
        }
      }
      # Done
      return $object;
    }
  }
  class Fruit {
    private $name, $color;
    public $price;
    function __construct( $name, $color ) {
      $this->name = $name;
      $this->color = $color;
    }
    function setName( $name ) {
      $this->name = $name;
    }
    function introduce() {
      print "Hello, this is an {$this->name} {$this->sirname}, its price is {$this->price} RMB.";
    }
  }
  # Setup a factory
  $fruit_factory = new FruitFactory('Fruit', 'Apple', 'Gonn');
  $fruit_factory->setName('Apple');
  $fruit_factory->price = 2;
  # Get an instance
  $apple = $fruit_factory->instance();
  $apple->introduce();
?>

Factory pattern is mainly to provide transition interface for creating objects, so as to shield and isolate the specific process of creating objects and achieve the purpose of improving flexibility.

Factory patterns can be divided into three categories:

Simple Factory Mode (Simple Factory) Factory Method Pattern (Factory Method) Abstract Factory Pattern (Abstract Factory)

These three patterns are gradually abstracted from top to bottom, and they are more generic.

Simple factory pattern is also called static factory method pattern. It can be seen from the naming that this mode 1 must be very simple. It exists for a simple purpose: to define an interface for creating objects.

The factory method pattern removes the static property of the factory method in the simple factory pattern so that it can be inherited by subclasses. Thus, the pressure of concentrating on the factory method in the simple factory pattern can be shared by the different factory subclasses in the factory method pattern.

The factory method pattern seems to have wrapped the creation of objects perfectly, so that only the interfaces provided by abstract product roles are dealt with in the client program. So do we have to spread the code all over the factory? You don't have to. Perhaps you can consider using the factory method pattern in the following situations:

When the client program does not need to know the creation process of the object to be used. The object used by the client program may change, or it doesn't know which specific object to use at all.

Summarize


Related articles: