


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
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(); }
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; } }
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; } }
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."; } }
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'); } } }
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 { }
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) { //... }
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());
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
