PHP计划模式之工场要领计划模式实例说明
发布时间:2021-05-23 18:25:58 所属栏目:编程 来源:网络整理
导读:本篇章节讲授PHP计划模式之工场要领计划模式。供各人参考研究详细如下: 一、什么是工场要领模式 作为一种建设型计划模式,工场要领模式就是要建设“某种对象”。对付工场要领,要建设的“对象”是一个产物,这个产物与建设它的类之间不存在绑定。
本篇章节讲授PHP计划模式之工场要领计划模式。分享给各人供各人参考,详细如下: 一、什么是工场要领模式作为一种建设型计划模式,工场要领模式就是要建设“某种对象”。对付工场要领,要建设的“对象”是一个产物,这个产物与建设它的类之间不存在绑定。现实上,为了保持这种松耦合,客户会通过一个工场发出哀求,再由工场建设所哀求的产物。操作工场要领模式,哀求者只发出哀求,而不详细建设产物。 二、什么时辰行使工场要领模式假如实例化工具的子类也许改变,就要行使工场要领模式。 三、一样平常工场要领模式行使一样平常工场要领模式时,客户只包括工场的引用,一个工场出产一种产物。增进一种产物的同时必要增进一个新工场类和一个新产物类。 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() . ''; $this->imageFactory = new ImageFactory(); echo $this->imageFactory->startFactory() . ' '; } } $client = new Client(); /*运行功效: 此处为文本 此处为图像 */ ?> 四、参数化工场要领模式行使参数化工场要领模式时,客户包括工场和产物的引用,发出哀求时必要指定产物的种类,一个工场出产多种产物。增进一种产物时只必要增进一个新产物类即可。 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) . ''; echo $factory->startFactory($imageProduct) . ' '; } } $client = new Client(); /*运行功效: 此处为文本 此处为图像 */ ?> 更多关于PHP相干内容感乐趣的读者可查察本站专题:《》、《》、《》、《》、《》、《》及《》 但愿本文所述对各人PHP措施计划有所辅佐。 (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐
热点阅读