php factory method pattern: 1. The factory method is to provide an independent factory class for a product, and create different product instances through different factory instances; 2. It has good encapsulation and clear code structure , has good scalability and shields product categories.
php factory method pattern:
1. What is the factory method pattern
Factory method It is to provide an independent factory class for a product, and create different product instances through different factory instances.
2. Advantages of the factory method pattern
It has good encapsulation and clear code structure. The creation of each object is conditionally restricted. For example: to call a specific product object, you only need to know the class name and constraint parameters of the product, without knowing the complex process of creating the object itself. Reduce the coupling between modules.
has good scalability. To add a new product category, you only need to add a factory class appropriately or extend a factory class. For example, in the following example, when you need to add a database Oracle To operate, you only need to add an Oracle class, and the factory class can complete system expansion without modifying tasks.
Block product categories. This feature is very important. The caller does not need to care about how the implementation of the product class changes. It only needs to care about the interface of the product. As long as the interface remains unchanged, the upper modules in the system should not change.
3. Usage scenarios
The connection mode (connectMode) and payment method (payMode) of Alipay, WeChat and UnionPay. Using the factory mode, the "customer" does not need to know the specific connection method and payment method, and only needs to call connectMode and payMode. Factory mode can be used for operations such as connection mode (connectMode) and query mode (selectMode) of
MySQL
, SQL Server
, Oracle
etc. Encapsulate.
Let’s look at the specific cases:
Product category:
//抽象产品类 abstract class DataBase { abstract function connect(); abstract function getOne(); } //具体产品类 class MySql extends DataBase { function connect() { return "MySQL连接对象返回"; } function getOne() { return "MySQL返回查询结果"; } } //具体产品类 class SqlServer extends DataBase { function connect() { return "SQL Server连接对象返回"; } function getOne() { return "SQL Server返回查询结果"; } }
Factory category:
//抽象工厂类 abstract class FactoryDataBase{ function createDataBase(){} } //具体工厂类 class FactoryMySql extends FactoryDataBase { public function createDataBase() { return new MySql(); } } //具体工厂类 class FactorySqlServer extends FactoryDataBase { public function createDataBase() { return new SqlServer(); } }
Customer:
$mysql = new FactoryMySql(); $db1 = $mysql->createDataBase();
4. The composition of the factory method pattern
Abstract factory role: This is the core of the factory method pattern, and it is related to the application The program has nothing to do with it. It is the interface that a specific factory role must implement or the parent class that must be inherited.
Specific factory role: It contains code related to specific business logic. Called by an application to create an object corresponding to a specific product.
Abstract product role: It is the parent class inherited by a specific product or the interface implemented.
Specific product role: The object created by the specific factory role is an instance of this role.
The factory method pattern seems to have perfectly packaged the creation of objects, so that the client program only processes the interface provided by the abstract product role. So do we have to have factories all over the code? No need to. Maybe you can consider using the factory method pattern in the following situations:
When the client program does not need to know the creation process of the object to be used.
The objects used by the client program may change, or they may not know which specific object to use.
Related learning recommendations: PHP programming from entry to proficiency
The above is the detailed content of php factory method pattern problem. For more information, please follow other related articles on the PHP Chinese website!