Definition and Usage of Factory Pattern in PHP Design Pattern

  • 2021-09-16 06:21:20
  • OfStack

This paper illustrates the factory pattern definition and usage of PHP design pattern. Share it for your reference, as follows:

Factory mode (Factory Design Pattern) is a creative design mode, Follow that open-closed principle, To modify closed, Open to extensions. The factory method (Factory Method) pattern is to create "something." For the factory method pattern, 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 will make a request through a factory, and then the factory will create the requested product. Alternatively, with the factory method pattern, the requester will only make the request, but not create the product.

Work in a factory

First, establish the interface of 1 factory

Factory.php


<?php
abstract class Factory
{
 // Abstract method of creating objects 
 protected abstract function createProduct();
 // The method calls createProduct Method returns 1 Product objects .
 public function start()
 {
   return $this->createProduct();
 }
}

start Method returns 1 product, which calls the createProduct So the implementation of createProduct builds and returns a product object based on the Product interface.

For example, all products have a common method getProperties() The following is the corresponding Product interface

Product.php


<?php
// Product interface 
interface Product
{
 public function getProperties();
}

Next, we will set up two factories, the text factory TextFactory and the image factory phptoFactory

TextFactory.php


<?php
include_once('Factory.php');
include_once('TextProduct.php');
class TextFactory extends Factory
{
 protected function createProduct()
 {
  $product = new TextProduct();
  return $product->getProperties();
 }
}

PhotoFactory.php


<?php
include_once('Factory.php');
include_once('PhotoProduct.php');
class PhotoFactory extends Factory
{
 protected function createProduct()
 {
  $product = new PhotoProduct();
  return $product->getProperties();
 }
}

As you can see, in the implementation of the factory method, the getProperties method introduces polymorphism (polymorphism), which will be used to return "text" or "image." Same as 1 getProperties() There are multiple (poly) different forms (morphs), which is called polymorphism. In this case, one of the forms returns text and the other returns an image.

You can put anything you want in the properties implementation, and the factory method design will create this object and return it to Client for use.

The following are the implementations of the two products

TextProduct.php


<?php
include_once('Product.php');
class TextProduct implements Product
{
 public function getProperties()
 {
  return " Here is the text product ";
 }
}

PhotoProduct.php


<?php
include_once('Product.php');
class PhotoProduct implements Product
{
 // This is the method that the product has 
 public function getProperties()
 {
  return " Here is the image product ";
 }
}

These two products implement abstract methods in the Product interface getProperties() ,

Customer (Client)

We don't want customers to make product requests directly. In fact, we want customers to make requests through the Factory factory interface. In this way, if we add products or factories in the future, customers can make the same requests to get more types of products without destroying the application:

Client.php


<?php
include_once('PhotoFactory.php');
include_once('TextFactory.php');
class Client
{
 public function __construct()
 {
  $this->somePhotoObject = new PhotoFactory();
  echo $this->somePhotoObject->start() . '<br />';
  $this->someTextObject = new TextFactory();
  echo $this->someTextObject->start() . '<br />';
 }
}
$worker = new Client();

Run Client. php and get the following results

Here is the image product
Here is the text product

Note: The Client object does not make requests directly to the product, but through the factory. Importantly, the customer does not implement the product features, leaving them to the product implementation.

Adjust products

The real value of design patterns is not to speed up operation, but to speed up development.

If the requirements change now and you need to modify the image product, you only need to modify the getProperties method of the corresponding product PhotoProduct

Object change looks simple, but Product's getProperties() Method still maintains the same interface and requests the factory to return 1 property object

Add new products and parameterized requests

The question is, if you want to add more images and text descriptions, is it necessary to add a new specific factory class every time you add a new area? This means adding a new plant and product for each new area. Therefore, we introduced the parametric chemical plant design pattern

One of the main differences between the parameterized plant design pattern and the generic plant design pattern is that the customer contains references to the plant and the product. In the parameterized request, the Client class must specify the product, not the product plant. createProduct() The parameters in the operation are passed into 1 product by the customer; So the customer must indicate the specific product it wants. However, this request is still made through the factory interface Factory. Therefore, although the customer contains a product reference, the customer is still separated from the product through Factory.

Multiple products in 1 factory (parametric chemical factory method)

For most requests, the parametric chemical factory method is simpler because the customer only needs to process one specific factory. The factory method operation has one parameter indicating the products to be created. In the original design, each product had its own factory, and no other passing parameter was needed; Product realization depends on the specific factory of each product.

New factory interface

Factory.php


<?php
abstract class Factory
{
 // Abstract method of creating objects 
 protected abstract function createProduct(Product $product);
 // The method consists of factoryMethod Method returns 1 Product objects .
 public function start($product)
 {
   return $this->createProduct($product);
 }
}

As you can see in this new Factory interface, create() And start() Each requires a parameter that specifies an Product object instead of a specific implementation of the Product interface, so you can accept any concrete instance of Product.

Factory concrete realization

The concrete creator class CommonFactory implements the createProduct() , as follows

CommonFactory.php


<?php
include_once('Factory.php');
include_once('Product.php');
class CommonFactory extends Factory
{
 protected function createProduct(Product $product)
 {
  return $product->getProperties();
 }
}

This class calls getProperties, the method of Product, to return the product to the customer.

New product

Product-specific changes do not change the original Product interface, or the original code


<?php
// Product interface 
interface Product
{
 public function getProperties();
}

For example, there is now a pen product PenProduct

PenProduct.php


<?php
// Product interface 
interface Product
{
 public function getProperties();
}

0

Customer Clent (with parameters)


<?php
// Product interface 
interface Product
{
 public function getProperties();
}

1

Output after running

Pen products

In the future, if a new product is developed, it is only necessary to create the corresponding product class, and then the customer specifies the desired new product, and then the product required by the customer can be returned.

Summary:

Product change: the interface remains unchanged

One of the great benefits of using design patterns is that you can easily make changes to classes without breaking larger programs. The secret to making changes easily is to keep the interface unchanged and change only the content.

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Introduction to PHP Basic Grammar", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

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


Related articles: