日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php设计模式 -- 工厂模式

發(fā)布時間:2024/4/15 php 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php设计模式 -- 工厂模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

工廠模式的最大優(yōu)點在于創(chuàng)建對象上面,就是把創(chuàng)建對象的過程封裝起來,這樣隨時可以產生一個新的對象。
減少代碼進行復制粘帖,耦合關系重,牽一發(fā)動其他部分代碼。

通俗的說,以前創(chuàng)建一個對象要使用new,現在把這個過程封裝起來了。
假設不使用工廠模式:那么很多地方調用類a,代碼就會這樣子創(chuàng)建一個實例:new a(),假設某天需要把a類的名稱修改,意味著很多調用的代碼都要修改。

工廠模式的優(yōu)點就在創(chuàng)建對象上。
工廠模式的優(yōu)點就在創(chuàng)建對象上。建立一個工廠(一個函數或一個類方法)來制造新的對象,它的任務就是把對象的創(chuàng)建過程都封裝起來,
創(chuàng)建對象不是使用new的形式了。而是定義一個方法,用于創(chuàng)建對象實例。

<?php namespace Libs;class Factory {public static function createDatabase(){$db = new Database();return $db;} }

調用時,不是使用 $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)建一系列相關或相互依賴對象的接口* 與工廠方法的區(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>";} } //本質區(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)建的對象較少,客戶只知道傳入工廠類的參數,對于如何創(chuàng)建對象不關心。

工廠方法模式:當一個類不知道它所必須創(chuàng)建對象的類或一個類希望由子類來指定它所創(chuàng)建的對象時,當類將創(chuàng)建對象的職責委托給多個幫助子類中得某一個,并且你希望將哪一個幫助子類是代理者這一信息局部化的時候,可以使用工廠方法模式。

抽象工廠模式:一個系統(tǒng)不應當依賴于產品類實例何如被創(chuàng)建,組合和表達的細節(jié),這對于所有形態(tài)的工廠模式都是重要的。這個系統(tǒng)有多于一個的產品族,而系統(tǒng)只消費其 中某一產品族。同屬于同一個產品族的產品是在一起使用的,這一約束必須在系統(tǒng)的設計中體現出來。系統(tǒng)提供一個產品類的庫,所有的產品以同樣的接口出現,從 而使客戶端不依賴于實現。

轉載于:https://www.cnblogs.com/mmmzh/p/10108374.html

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的php设计模式 -- 工厂模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。