php设计模式 -- 工厂模式
工廠模式的最大優(yōu)點在于創(chuàng)建對象上面,就是把創(chuàng)建對象的過程封裝起來,這樣隨時可以產(chǎn)生一個新的對象。
減少代碼進行復制粘帖,耦合關(guān)系重,牽一發(fā)動其他部分代碼。
通俗的說,以前創(chuàng)建一個對象要使用new,現(xiàn)在把這個過程封裝起來了。
假設(shè)不使用工廠模式:那么很多地方調(diào)用類a,代碼就會這樣子創(chuàng)建一個實例:new a(),假設(shè)某天需要把a類的名稱修改,意味著很多調(diào)用的代碼都要修改。
工廠模式的優(yōu)點就在創(chuàng)建對象上。
工廠模式的優(yōu)點就在創(chuàng)建對象上。建立一個工廠(一個函數(shù)或一個類方法)來制造新的對象,它的任務(wù)就是把對象的創(chuàng)建過程都封裝起來,
創(chuàng)建對象不是使用new的形式了。而是定義一個方法,用于創(chuàng)建對象實例。
調(diào)用時,不是使用 $db = new ?Libs\Database();
而是 $db = ?Libs\Factory::createDatabase();
*簡單工廠模式
<?php /*** 簡單工廠模式又叫靜態(tài)工廠方法模式,理解為可以通過一個靜態(tài)方法創(chuàng)建對象* */interface animal{function eat(); } class herbivore implements animal{function eat(){echo "吃草<br>";} } class carnivore implements animal{function eat(){echo "吃肉<br>";} } class omnivores implements animal{function eat(){echo "雜食<br>";} }class Demo{static function createHerbivore(){return new herbivore;}static function createCarnivore(){return new carnivore;}static function createOmnivores(){return new omnivores;} } $herbivore = Demo::createHerbivore(); $herbivore->eat(); $carnivore = Demo::createCarnivore(); $carnivore->eat(); $omnivores = Demo::createOmnivores(); $omnivores->eat();*工廠方法模式
<?php /*** 工廠模式 定義一個創(chuàng)建對象的接口,讓子類決定哪個類實例化,解決簡單工廠模式中封閉開發(fā)原則* */interface animal{function eat(); } class herbivore implements animal{function eat(){echo "吃草<br>";} } class carnivore implements animal{function eat(){echo "吃肉<br>";} } class omnivores implements animal{function eat(){echo "雜食<br>";} } //將對象的創(chuàng)建抽象成一個接口 interface createAnimal{function create(); } class FactoryHerbivore implements createAnimal{function create(){return new herbivore;} } class FactoryCarnivore implements createAnimal{function create(){return new carnivore;} } class FactoryOmnivores implements createAnimal{function create(){return new omnivores;} } class Demo{function test(){$Factory = new FactoryHerbivore();$herbivore = $Factory->create();$herbivore->eat();$Factory = new FactoryCarnivore();$carnivore = $Factory->create();$carnivore->eat();$Factory = new FactoryOmnivores();$omnivores = $Factory->create();$omnivores->eat();} }$demo = new Demo(); $demo->test();*抽象工廠方法
<?php /*** 抽象工廠 提供一個創(chuàng)建一系列相關(guān)或相互依賴對象的接口* 與工廠方法的區(qū)別,這里定義了一系列接口,工廠方法只有一個* * */interface animal{function eat(); } class yesHerbivore implements animal{function eat(){echo "吃草<br>";} } class noHerbivore implements animal{function eat(){echo "不吃草<br>";} } class yesCarnivore implements animal{function eat(){echo "吃肉<br>";} } class noCarnivore implements animal{function eat(){echo "不吃肉<br>";} } class yesOmnivores implements animal{function eat(){echo "雜食<br>";} } class noOmnivores implements animal{function eat(){echo "不雜食<br>";} } //本質(zhì)區(qū)別在這里,將對象的創(chuàng)建抽象成一個接口 interface createAnimal{function createYes();function createNo(); } class FactoryHerbivore implements createAnimal{function createYes(){return new yesHerbivore();}function createNo(){return new noHerbivore();} } class FactoryCarnivore implements createAnimal{function createYes(){return new yesCarnivore();}function createNo(){return new noCarnivore();} } class FactoryOmnivores implements createAnimal{function createYes(){return new yesOmnivores();}function createNo(){return new noOmnivores();} } class Demo{function test(){$Factory = new FactoryHerbivore();$herbivore = $Factory->createYes();$herbivore->eat();$herbivore = $Factory->createNo();$herbivore->eat();$Factory = new FactoryCarnivore();$carnivore = $Factory->createYes();$carnivore->eat();$carnivore = $Factory->createNo();$carnivore->eat();$Factory = new FactoryOmnivores();$omnivores = $Factory->createYes();$omnivores->eat();$omnivores = $Factory->createNo();$omnivores->eat();} }$demo = new Demo(); $demo->test();簡單工廠模式:工廠類負責創(chuàng)建的對象較少,客戶只知道傳入工廠類的參數(shù),對于如何創(chuàng)建對象不關(guān)心。
工廠方法模式:當一個類不知道它所必須創(chuàng)建對象的類或一個類希望由子類來指定它所創(chuàng)建的對象時,當類將創(chuàng)建對象的職責委托給多個幫助子類中得某一個,并且你希望將哪一個幫助子類是代理者這一信息局部化的時候,可以使用工廠方法模式。
抽象工廠模式:一個系統(tǒng)不應(yīng)當依賴于產(chǎn)品類實例何如被創(chuàng)建,組合和表達的細節(jié),這對于所有形態(tài)的工廠模式都是重要的。這個系統(tǒng)有多于一個的產(chǎn)品族,而系統(tǒng)只消費其 中某一產(chǎn)品族。同屬于同一個產(chǎn)品族的產(chǎn)品是在一起使用的,這一約束必須在系統(tǒng)的設(shè)計中體現(xiàn)出來。系統(tǒng)提供一個產(chǎn)品類的庫,所有的產(chǎn)品以同樣的接口出現(xiàn),從 而使客戶端不依賴于實現(xiàn)。
轉(zhuǎn)載于:https://www.cnblogs.com/mmmzh/p/10108374.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的php设计模式 -- 工厂模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: selenium基础框架的封装(Pyth
- 下一篇: TCGA样本命名详解