Home > Backend Development > PHP Tutorial > Examples to explain the simple factory pattern in PHP design pattern programming, examples to explain the design pattern_PHP tutorial

Examples to explain the simple factory pattern in PHP design pattern programming, examples to explain the design pattern_PHP tutorial

WBOY
Release: 2016-07-12 08:58:19
Original
1069 people have browsed it

Examples to explain the simple factory pattern in PHP design pattern programming, examples to explain the design pattern

The simple factory pattern is a class creation pattern, also called a static factory method (Static Factory Method) model. The simple factory pattern uses a factory object to determine which instance of the product class to create.

1. Several forms of factory pattern
The factory pattern is specifically responsible for instantiating a large number of classes with common interfaces. The factory pattern can dynamically decide which class to instantiate without having to know in advance which class to instantiate each time. Factory mode has the following forms:
(1) Simple Factory pattern, also known as Static Factory Method Pattern.
(2) Factory Method pattern, also known as Polymorphic Factory pattern or Virtual Constructor pattern;
(3) Abstract Factory pattern, also known as Kit or Toolkit pattern. The following is a simplified class diagram of the simple factory pattern.

Simple factory pattern, or static factory method pattern, is a special implementation of different factory method patterns. In other literature, simple factories are often discussed as a special case of the general factory pattern.
Learning the simple factory pattern is a good preparation for learning the factory method pattern, and it is also a good preparation for learning other patterns, especially the singleton pattern and the multiple instance pattern.

2. Introduction of simple factory model

For example, there is a farm company that specializes in selling various fruits to the market. The following fruits need to be described in this system:
Grape Grape
Strawberry
Apple Apple
Fruits are very different from other plants in that they can ultimately be picked and eaten. Then a natural approach is to create an interface that is applicable to all kinds of fruits in order to distinguish them from other plants on the farm. As shown below.

The fruit interface specifies the interfaces that all fruits must implement, including the methods that any fruit class must have: plant(), grow() and harvest(). The class diagram of interface Fruit is shown below.

The source code of this fruit interface is as follows.
Code Listing 1: Source code of interface Fruit

interface Fruit
{
public function grow();
public function harvest();
public function plant();
}
Copy after login

The Apple class is a type of fruit class, so it implements all the methods declared by the fruit interface. In addition, since apples are perennial plants, there is an additional treeAge property to describe the age of the apple tree. Below is the source code for this Apple class.
Code Listing 2: Apple-like source code

class Apple implements Fruit
{
private $_treeAge;
public function grow()
{
echo "Apple is growing.";
}
public function harvest()
{
echo "Apple has been harvested.";
}
public function plant()
{
echo "Apple has been planted.";
}
public function getTreeAge()
{
return $this->_treeAge;
}
public function setTreeAge($treeAge)
{
$this->_treeAge = (int) $treeAge;
}
}
Copy after login

Similarly, the Grape class is a type of fruit class and also implements all the methods declared by the Fruit interface. However, since grapes are divided into two types: seeded and seedless, they have one more seedless property than ordinary fruits, as shown in the figure below.

The source code of grape class is as follows. It can be seen that the Grape class also implements the Fruit interface and is therefore a subtype of the Fruit type.
Code Listing 3: Source code of class Grape

class Grape implements Fruit
{
private $seedless;
public function grow()
{
echo "Grape is growing.";
}
public function harvest()
{
echo "Grape has been harvested.";
}
public function plant()
{
echo "Grape has been planted.";
}
public function getSeedless()
{
return $this->seedless;
}
public function setSeedless($seedless)
{
$this->seedless = (boolean) $seedless;
}
}
Copy after login

The Strawberry class implements the Fruit interface and, therefore, is also a subtype of the fruit type. Its source code is as follows.
Code Listing 4: Source code of class Strawberry

class Strawberry implements Fruit
{
public function grow()
{
echo "Strawberry is growing.";
}
public function harvest()
{
echo "Strawberry has been harvested.";
}
public function plant()
{
echo "Strawberry has been planted.";
}
}
Copy after login

The gardener on the farm is also part of the system and should naturally be represented by a suitable class. This class is the FruitGardener class, and its structure is described by the following class diagram.

The FruitGardener class will create different fruit objects according to the client's requirements, such as instances of Apple, Grape or Strawberry. If an illegal request is received, the FruitGardener class will throw a BadFruitException exception.
The source code of the Gardener class is shown below.
Code Listing 5: Source code of FruitGardener class

class FruitGardener
{
public static function factory($which)
{
$which = strtolower($which);
if ($which == 'apple') {
return new Apple();
} elseif ($which == 'strawberry') {
return new Strawberry();
} elseif ($which == 'grape') {
return new Grape();
} else {
throw new BadFruitException('Bad fruit request');
}
}
}
Copy after login

As you can see, the Gardener class provides a static factory method. When called by the client, this method creates the fruit object required by the client. If the client's request is not supported by the system, the factory method will throw a BadFruitException. The source code of this exception class is shown below.
Code Listing 6: Source code of BadFruitException class

class BadFruitException extends Exception
{
}
Copy after login

When using it, the client only needs to call the static method factory() of FruitGardener. Please see the instructions below
sex client source code.
Code Listing 7: How to use the exception class BadFruitException

try {
FruitGardener::factory('apple');
FruitGardener::factory('grape');
FruitGardener::factory('strawberry');
//...
} catch (BadFruitException $e) {
//...
}
Copy after login

In this way, the farm will surely have a bumper harvest!

3. Design an "object-oriented" calculator using the simple factory pattern

/**
 * 面向对象计算器
 * 思路:
 * 1、面向对象的基本,封装、继承、多太
 * 2、父类公用类
 * 3、各种运算类
 */
 
/**
 * 基类,运算类
 * 只提供基本数据,不参与运算
 */
 
class Operation {
  
 // 第一个数
 public $first_num = 0;
  
 // 第二个数
 public $second_num = 0;
  
 /**
  * 获取结果,其他类覆盖此方法
  * @return double $result
  */
 public function getResult() {
  $result = 0.00;
   
  return $result;
 }
}
 
/**
 * 加法类
 */
class OperationAdd extends Operation {
 /**
  * 覆盖父类,实现加法算法
  */
 public function getResult() {
  $result = 0;
  return $this->first_num + $this->second_num;
 }
}
 
/**
 * 减法类
 *
 */
class OperationSub extends Operation {
 /**
  * 覆盖父类,实现加法算法
  */
 public function getResult() {
  $result = 0;
  return $this->first_num - $this->second_num;
 }
}
 
/**
 * 乘法类
 *
 */
class OperationMul extends Operation {
 /**
  * 覆盖父类,实现加法算法
  */
 public function getResult() {
  $result = 0;
  return $this->first_num * $this->second_num;
 }
}
 
/**
 * 除类
 *
 */
class OperationDiv extends Operation {
 /**
  * 覆盖父类,实现加法算法
  */
 public function getResult() {
  $result = 0;
   
  if ($this->second_num == 0) {
   throw new Exception('除法操作第二个参数不能为零!');
   return 0;
  }
   
  return $this->first_num / $this->second_num;
 }
}
 
/**
 * 工厂类
 */
class OperationFactory {
 /**
  * 工厂函数
  * @param string $operation
  * @return object
  */
 public function createOperation($operation) {
  $oper = null;
   
  switch($operation) {
   case '+':
    $oper = new OperationAdd();
    break;
   case '-':
    $oper = new OperationSub();
    break;
   case '*':
    $oper = new OperationMul();
    break;
   case '/':
    $oper = new OperationDiv();
    break;
   default:
    return 0;
  }
  return $oper;
 }
}
 
 
$operation = new OperationFactory();
$oper = $operation->createOperation('/');
 
$oper->first_num = 10;
$oper->second_num = 20;
var_dump($oper->getResult());
Copy after login

201622993823532.jpg (780×328)

Articles you may be interested in:

  • Detailed explanation of simple factory pattern of PHP design pattern
  • Explanation of example code of "simple factory pattern" in PHP
  • PHP Simple Complaint Page Example of Design Pattern
  • Observer Pattern Example of PHP Design Pattern
  • Delegation Pattern of PHP Design Pattern
  • Delegation Design Pattern of PHP Common Design Pattern
  • Specification specification pattern of PHP design pattern series
  • Learn PHP design pattern PHP implements Memento pattern (Memento)
  • Learn PHP design pattern PHP implements Observer pattern (Observer)
  • Learn php design pattern php implementation template method pattern

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1104325.htmlTechArticleExamples explain the simple factory pattern in PHP design pattern programming. Examples explain the design pattern. The simple factory pattern is a class creation pattern. , also called Static Factory Method model...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template