Home >Backend Development >PHP Tutorial >What is the php factory method pattern?
php The factory method pattern is a creational pattern and is one of the commonly used design patterns. The factory pattern is also divided into simple factory pattern, factory method pattern, static factory pattern and abstract factory pattern, and the factory method is to solve Simple factory scalability issues.
Belongs to: Creation mode, commonly used One of the design patterns
Factory patterns are divided into: simple factory pattern, factory method pattern, static factory pattern, and abstract factory pattern.
The following is the factory method pattern.
The factory method is to solve the scalability problem of the simple factory. I believe everyone will also find its flaws when looking at the simple factory.
In the past, the simple factory had to be expanded When , it is necessary to modify the factory content, which violates the principle that the design pattern is open for external expansion and closed for internal modification, so I created a factory method pattern. This is my understanding.
Involved elements:
Factory class (instantiate product A)
Factory class (instantiate product B)
Product class A
Product class B
A mall website supports multiple payment functions, such as: Alipay, WeChat, a certain bank, a certain bank... How to design the program.
If you follow the simple factory model and declare a factory class, and let the factory class determine which payment method to call, then there must be a continuous modification operation of the factory class. Every time a payment method is added, the factory class must be modified. Violates the [open and closed] principle
Problems to be solved:
1. Solve the [open and closed] principle problem of simple factory
The flow chart is as follows:
credcred: `##1. Test entrance index.php
<?php namespace factory_method; include '../autoload.php'; // 张三支付 $param = []; $param['name'] = '张三'; $param['money'] = 100; $factory = new WeiXinFactory(); $mode = $factory->createMode(); // 使用工厂类获取产品类 $mode->doAction($param); // 执行产品类方法2. Factory class interface, Alipay payment Factory class, WeChat payment factory class, UnionPay payment factory class.
Factory class interface: PayFactoryInterface.php
<?php namespace factory_method;/** * 工厂类接口 * Interface PayFactoryInterface * @package factory_method */interface PayFactoryInterface { public function createMode(); }
Alipay payment factory class: ZhiFuBaoFactory.php
<?php namespace factory_method;/** * 支付宝产品类转工厂类 * Class ZhiFuBaoFactory * @package factory_method */class ZhiFuBaoFactory implements PayFactoryInterface { public function createMode() { return new ZhiFuBao(); } }
WeChat payment factory class: WeiXinFactory.php
<?php namespace factory_method;/** * 微信产品类转为工厂类 * Class WeiXinFactory * @package factory_method */class WeiXinFactory implements PayFactoryInterface { public function createMode() { return new WeiXin(); } }
UnionPay payment factory class: YinLianFactory.php
<?php namespace factory_method;/** * 银联产品类转工厂类 * Class YinLianFactory * @package factory_method */class YinLianFactory implements PayFactoryInterface { public function createMode() { return new Yinlian(); } }
3. Product class interface, Alipay payment product class, WeChat payment product class, UnionPay payment product class.
Product interface: PayInterface.php
<?php namespace factory_method;/** * Interface PayInterface * 定义支付Api规范接口 */interface PayInterface { /** * 发起扣款 * @return mixed */ public function doAction($param); /** * 记录日志 * @return mixed */ public function payLog(); /** * 发送短信 * @return mixed */ public function sendNews(); }
Alipay payment product category: ZhiFuBao.php
<?php namespace factory_method;// 银联class ZhiFuBao implements PayInterface { public function doAction($param) { $name = $param['name']; $money = $param['money']; echo $name . '使用支付宝支付,支付金额' . $money; } /** * 记录日志 */ public function payLog() { } /** * 发送消息 */ public function sendNews() { } }
WeChat payment product category: WeiXin.php
<?php namespace factory_method;// 银联class WeiXin implements PayInterface { public function doAction($param) { $name = $param['name']; $money = $param['money']; echo $name . '使用微信支付,支付金额' . $money; } /** * 记录日志 */ public function payLog() { } /** * 发送消息 */ public function sendNews() { } }
UnionPay payment product category: Yinlian.php
<?php namespace factory_method;// 银联class Yinlian implements PayInterface { public function doAction($param) { $name = $param['name']; $money = $param['money']; echo $name . '使用银联支付,支付金额' . $money; } /** * 记录日志 */ public function payLog() { } /** * 发送消息 */ public function sendNews() { } }
The following is the source code address
GitHub source code address: https://github.com/xiaobaoword/design_pattern/tree/master
The above is the detailed content of What is the php factory method pattern?. For more information, please follow other related articles on the PHP Chinese website!