PHP设计模式一:工厂方法设计模式
一、什么是工厂方法模式
作为一种创建型设计模式,工厂方法模式就是要创建“某种东西”。对于工厂方法,要创建的“东西”是一个产品,这个产品与创建它的类之间不存在绑定。实际上,为了保持这种松耦合,客户会通过一个工厂发出请求,再由工厂创建所请求的产品。利用工厂方法模式,请求者只发出请求,而不具体创建产品。
二、什么时候使用工厂方法模式
如果实例化对象的子类可能改变,就要使用工厂方法模式。
三、一般工厂方法模式
使用一般工厂方法模式时,客户只包含工厂的引用,一个工厂生产一种产品。增加一种产品的同时需要增加一个新工厂类和一个新产品类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | <?php /** * 一般工厂方法设计模式 **/ //工厂抽象类 abstract class Factory { protected abstract function produce(); public function startFactory() { $pro = $this ->produce(); return $pro ; } } //文本工厂 class TextFactory extends Factory { protected function produce() { $textProduct = new TextProduct(); return $textProduct ->getProperties(); } } //图像工厂 class ImageFactory extends Factory { protected function produce() { $imageProduct = new ImageProduct(); return $imageProduct ->getProperties(); } } //产品类接口 interface Product { public function getProperties(); } //文本产品 class TextProduct implements Product { private $text ; function getProperties() { $this ->text = "此处为文本" ; return $this ->text; } } //图像产品 class ImageProduct implements Product { private $image ; function getProperties() { $this ->image = "此处为图像" ; return $this ->image; } } //客户类 class Client { private $textFactory ; private $imageFactory ; public function __construct() { $this ->textFactory = new TextFactory(); echo $this ->textFactory->startFactory() . '<br />' ; $this ->imageFactory = new ImageFactory(); echo $this ->imageFactory->startFactory() . '<br />' ; } } $client = new Client(); ?> |
四、参数化工厂方法模式
使用参数化工厂方法模式时,客户包含工厂和产品的引用,发出请求时需要指定产品的种类,一个工厂生产多种产品。增加一种产品时只需要增加一个新产品类即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <?php /** * 参数化工厂方法设计模式 **/ //工厂抽象类 abstract class Factory { protected abstract function produce(Product $product ); public function startFactory(Product $product ) { $pro = $this ->produce( $product ); return $pro ; } } //工厂实现 class ConcreteFactory extends Factory { protected function produce(Product $product ) { return $product ->getProperties(); } } //产品类接口 interface Product { public function getProperties(); } //文本产品 class TextProduct implements Product { private $text ; public function getProperties() { $this ->text = "此处为文本" ; return $this ->text; } } //图像产品 class ImageProduct implements Product { private $image ; public function getProperties() { $this ->image = "此处为图像" ; return $this ->image; } } //客户类 class Client { private $factory ; private $textProduct ; private $imageProduct ; public function __construct() { $factory = new ConcreteFactory(); $textProduct = new TextProduct(); $imageProduct = new ImageProduct(); echo $factory ->startFactory( $textProduct ) . '<br />' ; echo $factory ->startFactory( $imageProduct ) . '<br />' ; } } $client = new Client(); ?> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)