首页IT科技浅谈php设计模式的建造者模式研究(浅谈PHP设计模式的建造者模式)

浅谈php设计模式的建造者模式研究(浅谈PHP设计模式的建造者模式)

时间2025-06-17 12:06:54分类IT科技浏览4344
导读:简介: 建造者模式,又称之为生成器模式,属于创建型的设计模式。将一个复杂对象的构建,与它的表示分离,使得同样的构建过程可以创建不同的表示。 适用场景:...

简介:

建造者模式            ,又称之为生成器模式                   ,属于创建型的设计模式            。将一个复杂对象的构建      ,与它的表示分离            ,使得同样的构建过程可以创建不同的表示                  。

适用场景:

用于创建一些复杂的对象                   ,这些对象内部构建间的建造顺序通常是稳定的(这就表名可以抽离)      ,但对象的外在面临着复杂的变化       。

优点:

创建和表象分离

缺点:

如果核心类内部发生变化      ,建造者也要相应修改

与工厂模式:

比工厂模式多了一道自行处理的工序 代码: abstract class TestPaper { abstract public function BuildPaper(); abstract public function BuildQuestion(); } class ChineseExaminationPaper extends TestPaper { public function BuildPaper() { echo "使用A4纸"; } public function BuildQuestion() { echo "语文题"; } } class EnglishExaminationPaper extends TestPaper { public function BuildPaper() { echo "使用A6纸"; } public function BuildQuestion() { echo "英文题"; } } class ExaminationPaper { private $examination_paper; function __construct($examination_paper) { $this->examination_paper = $examination_paper; } public function create() { $this->examination_paper->BuildPaper(); $this->examination_paper->BuildQuestion(); } } //客户端代码 $thinDirector = new ExaminationPaper(new ChineseExaminationPaper()); $thinDirector->create(); $fatDirector = new ExaminationPaper(new EnglishExaminationPaper()); $fatDirector->create();

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
vue主流的ui组件库(Vue3 UI组件库对比,Naive UI、Element Plus、 Ant Design Vue) 总结部署wordpress博客平台的步骤(轻松搭建个人博客:使用WordPress一键安装脚本)