


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

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function