php设计模式 -- 工厂模式
工廠模式的最大優點在于創建對象上面,就是把創建對象的過程封裝起來,這樣隨時可以產生一個新的對象。
減少代碼進行復制粘帖,耦合關系重,牽一發動其他部分代碼。
通俗的說,以前創建一個對象要使用new,現在把這個過程封裝起來了。
假設不使用工廠模式:那么很多地方調用類a,代碼就會這樣子創建一個實例:new a(),假設某天需要把a類的名稱修改,意味著很多調用的代碼都要修改。
工廠模式的優點就在創建對象上。
工廠模式的優點就在創建對象上。建立一個工廠(一個函數或一個類方法)來制造新的對象,它的任務就是把對象的創建過程都封裝起來,
創建對象不是使用new的形式了。而是定義一個方法,用于創建對象實例。
調用時,不是使用 $db = new ?Libs\Database();
而是 $db = ?Libs\Factory::createDatabase();
*簡單工廠模式
<?php /*** 簡單工廠模式又叫靜態工廠方法模式,理解為可以通過一個靜態方法創建對象* */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 /*** 工廠模式 定義一個創建對象的接口,讓子類決定哪個類實例化,解決簡單工廠模式中封閉開發原則* */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>";} } //將對象的創建抽象成一個接口 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 /*** 抽象工廠 提供一個創建一系列相關或相互依賴對象的接口* 與工廠方法的區別,這里定義了一系列接口,工廠方法只有一個* * */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>";} } //本質區別在這里,將對象的創建抽象成一個接口 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();簡單工廠模式:工廠類負責創建的對象較少,客戶只知道傳入工廠類的參數,對于如何創建對象不關心。
工廠方法模式:當一個類不知道它所必須創建對象的類或一個類希望由子類來指定它所創建的對象時,當類將創建對象的職責委托給多個幫助子類中得某一個,并且你希望將哪一個幫助子類是代理者這一信息局部化的時候,可以使用工廠方法模式。
抽象工廠模式:一個系統不應當依賴于產品類實例何如被創建,組合和表達的細節,這對于所有形態的工廠模式都是重要的。這個系統有多于一個的產品族,而系統只消費其 中某一產品族。同屬于同一個產品族的產品是在一起使用的,這一約束必須在系統的設計中體現出來。系統提供一個產品類的庫,所有的產品以同樣的接口出現,從 而使客戶端不依賴于實現。
轉載于:https://www.cnblogs.com/mmmzh/p/10108374.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的php设计模式 -- 工厂模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: selenium基础框架的封装(Pyth
- 下一篇: 深入理解PHP的运行模式