php设计模式(3)工厂模式之工厂方法模式

2017-03-05 18:12:00 admin ...

<h1 style="position: relative; margin: 1em 0px; font-size: 18px; line-height: 24px; padding-left: 12px; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">前言:</h1><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">上一节我们讨论了简单工厂模式,这一节我们继续讨论工厂模式中的第二种模式——工厂方法模式。需要说明一下,简单工厂模式比较常用,工厂方法和抽象工厂模式用的比较少一些。</p><h1 style="position: relative; margin: 1em 0px; font-size: 18px; line-height: 24px; padding-left: 12px; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">内容概要:</h1><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">1、什么是是工厂方法模式</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">2、怎么实现工厂方法模式</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">3、示例代码</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);"><span style="font-size: 18px;">一、什么是工厂方法模式</span></p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">工厂方法模式是对每一种产品(相关联的产品)提供一个工厂类,调用放通过不同的工厂类来创建不同的产品实例。</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">举个例子,上回我们说到的冰激凌工厂现在扩大规模,投资在另一个地方又建立了一个冰激凌工厂,生产的产品都是冰激凌,产品种类也都是一样的:苹果味的和牛奶味的。但是这两种产品的生产地址是不同的。我们希望客户能够就近购买冰激凌。这时候就需要使用工厂方法模式了,通过不同的工厂类创建获得不同的产品实例(冰激凌)。</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">一句话:冰激凌开设分厂的时候用到工厂方法模式。</p><h1 style="position: relative; margin: 1em 0px; font-size: 18px; line-height: 24px; padding-left: 12px; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">二、怎么实现工厂方法模式</h1><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">实现工厂方法,牵扯到几个概念:抽象工厂,具体工厂,抽象产品,具体产品,我们一起了解一下。</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">1、抽象工厂,我们可以理解成两个冰激凌工厂的上级部门,这里假设称为总公司。</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">2、具体工厂,两个冰激凌工厂.</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">3、抽象产品,我们说“冰激凌”这个词就是抽象产品,这个词不能吃,但是却规定了冰激凌生产的配料标准,操作规范,冰激凌重量尺寸等等。</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">4、具体产品,用户实实在在拿在手里可以吃的冰激凌。</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">好了,对应好关系后,我们开始和php编码联系一下。</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">首先我们需要一个工厂的接口(或者一个抽象类,或两者都有)类,用于给各个具体工厂规定条条框框。也就是说,每个冰激凌分厂,要根据总公司的规定生产。</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">其次,我们给每个具体工厂创建一个类,用于实例化产品,必须实现工厂接口。</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">然后,我们还要建立一个产品的抽象类(或者接口,或者两者都有。)内容规定产品的规格等的代码</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">最后,创建具体产品的类描,必须继承产品抽象类。</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">大家可以找个UML图看看,我这里就不画了。</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">有点晕吧?恩,看代码的命!</p><h1 style="position: relative; margin: 1em 0px; font-size: 18px; line-height: 24px; padding-left: 12px; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">三、示例代码</h1><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">我把类全写在一个文件中哈,理由参考上一节。</p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);"><img src="/Uploads/ueditor/image/20170305/1488708654980548.png" title="1488708654980548.png" alt="blob.png"/></p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);"><img src="/Uploads/ueditor/image/20170305/1488708677261036.png" title="1488708677261036.png" alt="blob.png"/></p><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);"><img src="/Uploads/ueditor/image/20170305/1488708691911401.png" title="1488708691911401.png" alt="blob.png"/></p><hr/><p style="margin-top: 1em; margin-bottom: 1em; word-wrap: break-word; text-align: justify; line-height: 1.5; color: rgb(93, 93, 93); font-family: helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei", simsun; white-space: normal; background-color: rgb(255, 255, 255);">码字不容易,尊重版权,今日头条首发,转载请注明出处:头条号|PHP在路上。</p><p><br/></p>

相似文章


345708673

在路上

技术无止境,一直在路上。 年过而立,惴惴不安,愈加发奋,孜孜求学。