Explanation of Factory Pattern of PHP Design Pattern

  • 2021-08-10 07:13:37
  • OfStack

When developing large-scale systems, such a situation often occurs:

I have 1 part of the basic data, is the class classA is read from the database A, many other functions are based on this basic data to operate. Now, I want to change the data from the database A to another data source to get it. At this time, it is more troublesome to modify it, and many other classes of code have to be modified. This design is obviously not flexible enough, in other words, it is tightly coupled, so what is tight coupling? Tight coupling means that functions or classes in one part of the system depend heavily on the behavior and structure of functions or classes in other parts of the system.

At this time, the function of the factory model is reflected.

Factory model

It is the design method to solve such a situation.

The factory pattern is a class that builds a factory to create objects as needed, an approach that is important in polymorphic programming, allowing dynamic substitution of classes, modification of configurations, and so on.

/* Basic factory pattern code */


<?php 
/** 
 *  Basic factory model  
 * */ 
class User { 
 private $username; 
 public function __construct($username) { 
  $this->username = $username; 
 } 
  
 public function getUser() { 
  return $this->username; 
 } 
} 
 
class userFactory { 
 static public function createUser() { 
  return new User('Jack'); 
 } 
} 
 
$user = userFactory::createUser();echo $user->getUser(); 

?>

Factory patterns are divided into simple factory patterns, factory method patterns and abstract factory patterns.

Simple factory pattern, creating objects through static methods. It can be understood that you are only responsible for producing any 1 product in the same 1-level structure, but you cannot add new products.


<?php

/** 
 * Simple factory model  
 * */ 
interface userProperties { 
 function getUsername(); 
 function getGender(); 
 function getJob(); 
} 
class User implements userProperties{ 
 private $username; 
 private $gender; 
 private $job; 
 public function __construct($username, $gender, $job) { 
  $this->username = $username; 
  $this->gender = $gender; 
  $this->job = $job; 
 } 
 
 public function getUsername() { 
  return $this->username; 
 } 
 
 public function getGender() { 
  return $this->gender; 
 } 
 
 public function getJob() { 
  return $this->job; 
 } 
} 
 
class userFactory { 
 static public function createUser($properties = []) { 
  return new User($properties['username'], $properties['gender'], $properties['job']); 
 } 
} 
 
$employers = [ 
 ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'], 
 ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'], 
 ]; 
$user = userFactory::createUser($employers[0]); 
echo $user->getUsername(); 
 
?>

Factory method pattern removes the static attributes of methods in simple factory pattern, so that they can be integrated by subclasses, and defines an interface for creating objects, so that subclasses can decide which class to instantiate. It can be understood that it is used to produce fixed products in the same level structure, but supports additional products.


<?php
/** 
 *  Factory method pattern  
 **/ 
interface userProperties { 
 function getUsername(); 
 function getGender(); 
 function getJob(); 
} 
 
interface createUser { 
 function create($properties); 
} 
 
class User implements userProperties{ 
 private $username; 
 private $gender; 
 private $job; 
 public function __construct($username, $gender, $job) { 
  $this->username = $username; 
  $this->gender = $gender; 
  $this->job = $job; 
 } 
 
 public function getUsername() { 
  return $this->username; 
 } 
 
 public function getGender() { 
  return $this->gender; 
 } 
 
 public function getJob() { 
  return $this->job; 
 } 
} 
 
class userFactory { 
 private $user; 
 public function __construct($properties = []) { 
  $this->user = new User($properties['username'], $properties['gender'], $properties['job']); 
 } 
 
 public function getUser() { 
  return $this->user; 
 } 
} 
 
class FactoryMan implements createUser { 
 function create($properties) { 
  return new userFactory($properties); 
 } 
} 
 
class FactoryWoman implements createUser { 
 function create($properties) { 
  return new userFactory($properties); 
 } 
} 
 
class clientUser { 
 static public function getClient($properties) { 
  $fac = new FactoryMan; 
  $man = $fac->create($properties); 
  echo $man->getUser()->getUsername(); 
 } 
} 
 
$employers = [ 
 ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'], 
 ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'], 
 ]; 
$user = clientUser::getClient($employers[0]); 
 
?>

Abstract factory pattern, which provides an interface to create a series of related or interdependent objects. It can be understood that it is used to produce all products of different types, but new products cannot be added, and new types are supported.


<?php

/** 
 *  Abstract factory pattern  
 * */ 
 
interface userProperties { 
 function getUsername(); 
 function getGender(); 
 function getJob(); 
} 
 
interface createUser { // Abstracting the creation of an object into 1 Interface  
 function createOpen($properties);// Inward creation  
 function createIntro($properties);// Extroverted creation  
} 
 
class User implements userProperties{ 
 private $username; 
 private $gender; 
 private $job; 
 public function __construct($username, $gender, $job) { 
  $this->username = $username; 
  $this->gender = $gender; 
  $this->job = $job; 
 } 
 
 public function getUsername() { 
  return $this->username; 
 } 
 
 public function getGender() { 
  return $this->gender; 
 } 
 
 public function getJob() { 
  return $this->job; 
 } 
} 
 
class userFactory { 
 private $user; 
 public function __construct($properties = []) { 
  $this->user = new User($properties['username'], $properties['gender'], $properties['job']); 
 } 
 
 public function getUser() { 
  return $this->user; 
 } 
} 
 
class FactoryMan implements createUser { 
 function createOpen($properties) { 
  return new userFactory($properties); 
 } 
 
 function createIntro($properties) { 
  return new userFactory($properties); 
 } 
} 
 
class FactoryWoman implements createUser { 
 function createOpen($properties) { 
  return new userFactory($properties); 
 } 
 
 function createIntro($properties) { 
  return new userFactory($properties); 
 } 
} 
 
class clientUser { 
 static public function getClient($properties) { 
  $fac = new FactoryMan; 
  $man = $fac->createOpen($properties); 
  echo $man->getUser()->getUsername(); 
 } 
} 
 
$employers = [ 
 ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'], 
 ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'], 
 ]; 
$user = clientUser::getClient($employers[0]); 
?>

If there are any mistakes, please correct them.


Related articles: