Case Analysis of Factory Method Design Pattern Based on PHP Design Pattern

  • 2021-09-24 21:37:17
  • OfStack

This paper describes the factory method design pattern of PHP design pattern with examples. Share it for your reference, as follows:

1. What is the factory approach pattern

As a creative design pattern, the factory method pattern is to create "something". For the factory method, the "thing" to be created is a product, and there is no binding between this product and the class that created it. In fact, in order to maintain this loose coupling, the customer makes a request through a factory, and then the factory creates the requested product. With the factory method pattern, the requestor only makes the request, but does not create the product concretely.

2. When to use the factory method pattern

If the subclass of the instantiated object may change, use the factory method pattern.

3. 1 generic factory approach model

When using the 1-factory method pattern, the customer contains only a reference to the factory, and one factory produces one product. Adding one product requires adding one new factory class and one new product class.


<?php
/**
*  1 General factory method design pattern 
**/
// Factory abstract class 
abstract class Factory
{
  protected abstract function produce();
  public function startFactory()
  {
    $pro = $this->produce();
    return $pro;
  }
}
// Text factory 
class TextFactory extends Factory
{
  protected function produce()
  {
    $textProduct = new TextProduct();
    return $textProduct->getProperties();
  }
}
// Image factory 
class ImageFactory extends Factory
{
  protected function produce()
  {
    $imageProduct = new ImageProduct();
    return $imageProduct->getProperties();
  }
}
// Product class interface 
interface Product
{
  public function getProperties();
}
// Text product 
class TextProduct implements Product
{
  private $text;
  function getProperties()
  {
    $this->text = " Text here ";
    return $this->text;
  }
}
// Image product 
class ImageProduct implements Product
{
  private $image;
  function getProperties()
  {
    $this->image = " Here is an image ";
    return $this->image;
  }
}
// Customer category 
class Client
{
  private $textFactory;
  private $imageFactory;
  public function __construct()
  {
    $this->textFactory = new TextFactory();
    echo $this->textFactory->startFactory() . '<br />';
    $this->imageFactory = new ImageFactory();
    echo $this->imageFactory->startFactory() . '<br />';
  }
}
$client = new Client();
/* Run results: 
 Text here 
 Here is an image 
*/
?>

4. Parametric chemical plant method model

When using parametric chemical plant method mode, customers include references to factories and products, and need to specify the types of products when making requests. One factory produces multiple products. When adding one product, only one new product class needs to be added.


<?php
/**
*   Method design mode of parametric chemical plant 
**/
// Factory abstract class 
abstract class Factory
{
  protected abstract function produce(Product $product);
  public function startFactory(Product $product)
  {
    $pro = $this->produce($product);
    return $pro;
  }
}
// Factory implementation 
class ConcreteFactory extends Factory
{
  protected function produce(Product $product)
  {
    return $product->getProperties();
  }
}
// Product class interface 
interface Product
{
  public function getProperties();
}
// Text product 
class TextProduct implements Product
{
  private $text;
  public function getProperties()
  {
    $this->text = " Text here ";
    return $this->text;
  }
}
// Image product 
class ImageProduct implements Product
{
  private $image;
  public function getProperties()
  {
    $this->image = " Here is an image ";
    return $this->image;
  }
}
// Customer category 
class Client
{
  private $factory;
  private $textProduct;
  private $imageProduct;
  public function __construct()
  {
    $factory = new ConcreteFactory();
    $textProduct = new TextProduct();
    $imageProduct = new ImageProduct();
    echo $factory->startFactory($textProduct) . '<br />';
    echo $factory->startFactory($imageProduct) . '<br />';
  }
}
$client = new Client();
/* Run results: 
 Text here 
 Here is an image 
*/
?>

More readers interested in PHP can check out the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: