亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

深入分析PHP設(shè)計模式

瀏覽:144日期:2022-09-09 18:14:05

1、單例模式

一個類,只能允許有一個對象存在

<?phpclass test{ protected function __construct(){ } public static function getInstance(){ $_test = new test(); return $_test; }}$test = test::getInstance();var_dump($test);?>

2、工廠模式

工廠模式,顧名思義,如同工廠一樣,你把原材料放入工廠中,出來的是成品,而你并不需要知道工廠里做了什么,工廠模式主要用于解耦。

把對象的創(chuàng)建和使用的過程分開,比如: ClassA 調(diào)用 ClassB,那么 ClassA 只調(diào)用ClassB 的方法,至于實例化 ClassB 則在工廠內(nèi)實現(xiàn)。這樣既減少了代碼的重復(fù)使用,也方便對 ClassB 的后期維護。如果 ClassB 實例化過程很復(fù)雜,使用簡單工廠模式就會發(fā)現(xiàn)外部無需關(guān)注復(fù)雜的實例化,只管調(diào)用 ClassB 的方法即可,減少錯誤

interface mysql{ public function connect();} class mysqli2 implements mysql{ public function connect(){ echo ’mysqli’; }} class pdo2 implements mysql{ public function connect(){ echo ’pdo’; }}class mysqlFactory{ static public function factory($class_name){ return new $class_name(); }}$obj = mysqlFactory::factory(’pdo2’);$obj->connect();

3、注冊模式

注冊模式,解決全局共享和交換對象。已經(jīng)創(chuàng)建好的對象,掛在到某個全局可以使用的數(shù)組上,

在需要使用的時候,直接從該數(shù)組上獲取即可。將對象注冊到全局的樹上。任何地方直接去訪問。

<?phpclass Register{ protected static $objects; function set($alias,$object)//將對象注冊到全局的樹上 { self::$objects[$alias]=$object;//將對象放到樹上 } static function get($name){ return self::$objects[$name];//獲取某個注冊到樹上的對象 } function _unset($alias){ unset(self::$objects[$alias]);//移除某個注冊到樹上的對象。 }}AutoRegister::set(’single’,$single);$single = AutoRegister::get(’single’);var_dump($single);

4、適配器模式

將一個類的接口轉(zhuǎn)換成客戶希望的另外一個接口。

//目標(biāo)角色interface Aims{ public function newMethod1(); public function newMethod2();} //需要被適配的類(Adaptee)Class Man{ public function oldMethod1() { echo ’man’; } public function oldMethod2() { echo ’男人’; }} //需要被適配的類(Adaptee)Class Woman{ public function oldMethod1() { echo ’woman’; } public function oldMethod2() { echo ’女人’; }} //適配器,Class Adapters implements Aims{ private $adaptee; public function __construct($adaptee) { $this->adaptee = $adaptee; } public function newMethod1() { //以少量的代碼對被適配者作出適配 echo ’sex :’; $this->adaptee->oldMethod1(); } public function newMethod2() { echo ’sex name :’; $this->adaptee->oldMethod2(); }} $adapter1 = new Adapters(new Man);$adapter1->newMethod1();$adapter2 = new Adapters(new Woman);$adapter2->newMethod2();

5、策略模式

這是一個男人和女人的問題,將一組特定的行為和算法封裝成類,以適應(yīng)某些特定的上下文環(huán)境。

UserStrategy.php<?php/* * 聲明策略文件的接口,約定策略包含的行為。 */interface UserStrategy{ function showAd(); function showCategory();}FemaleUser.php<?phpclass FemaleUser implements UserStrategy{ function showAd(){ echo '2016冬季女裝'; } function showCategory(){ echo '女裝'; }}MaleUser.php<?phpclass MaleUser implements UserStrategy{ function showAd(){ echo 'IPhone6s'; } function showCategory(){ echo '電子產(chǎn)品'; }}Page.php//執(zhí)行文件<?phprequire_once ’Loader.php’;class Page{ protected $strategy; function index(){ echo 'AD'; $this->strategy->showAd(); echo '<br>'; echo 'Category'; $this->strategy->showCategory(); echo '<br>'; } function setStrategy(UserStrategy $strategy){ $this->strategy=$strategy; }}$page = new Page();if(isset($_GET[’male’])){ $strategy = new MaleUser();}else { $strategy = new FemaleUser();}$page->setStrategy($strategy);$page->index();

6、原型模式

不常用,大的對象類才使用,表現(xiàn)在clone

7、觀察者模式

從面向過程的角度來看,首先是觀察者向主題注冊,注冊完之后,主題再通知觀察者做出相應(yīng)的操作,整個事情就完了

/** * 事件產(chǎn)生類 * Class EventGenerator */abstract class EventGenerator{ private $ObServers = []; //增加觀察者 public function add(ObServer $ObServer) { $this->ObServers[] = $ObServer; } //事件通知 public function notify() { foreach ($this->ObServers as $ObServer) { $ObServer->update(); } }}/** * 觀察者接口類 * Interface ObServer */interface ObServer{ public function update($event_info = null);}/** * 觀察者1 */class ObServer1 implements ObServer{ public function update($event_info = null) { echo '觀察者1 收到執(zhí)行通知 執(zhí)行完畢!n'; }}/** * 觀察者1 */class ObServer2 implements ObServer{ public function update($event_info = null) { echo '觀察者2 收到執(zhí)行通知 執(zhí)行完畢!n'; }}/** * 事件 * Class Event */class Event extends EventGenerator{ /** * 觸發(fā)事件 */ public function trigger() { //通知觀察者 $this->notify(); }}//創(chuàng)建一個事件$event = new Event();//為事件增加旁觀者$event->add(new ObServer1());$event->add(new ObServer2());//執(zhí)行事件 通知旁觀者$event->trigger();

以上就是深入分析PHP設(shè)計模式的詳細內(nèi)容,更多關(guān)于PHP設(shè)計模式的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: PHP
相關(guān)文章:
主站蜘蛛池模板: 国产99re在线观看只有精品 | 亚洲国产99在线精品一区二区 | 99久久精品免费精品国产 | 久久成人免费网站 | 一级做a毛片免费视频 | 日本黄色一级片视频 | 亚洲日韩欧美制服二区dvd | 欧美日韩一区二区三区在线 | 欧美一欧美一级毛片 | 国产曰批视频免费观看完 | 一级做a爱片特黄在线观看 一级做a爱片特黄在线观看免费看 | 国产一区精品在线 | a国产视频 | 欧美伦理片在线观看 | 国产精品视频一区日韩丝袜 | 99爱在线视频这里只有精品 | 国产婷婷色一区二区三区深爱网 | 国产欧美一区二区三区久久 | 国产无限制自拍 | 国产污视频在线观看 | 视频在线观看一区 | 黄在线网站 | 小明看看成人免费 | 亚洲精品中文字幕乱码影院 | 亚洲欧美综合国产不卡 | 国产综合精品 | 欧美一级视频在线高清观看 | 中文字幕日本亚洲欧美不卡 | 亚洲第一a亚洲 | 国产免费高清无需播放器 | 色天天影视 | 国产精品免费久久 | 国产精品一区伦免视频播放 | 狼人综合伊人网 | 黄色美女一级片 | 黄色片三级网站 | 特黄特色的大片观看免费视频 | 精品精品久久宅男的天堂 | 在线麻豆国产传媒60在线观看 | 久久国产精品男女热播 | 久久成人免费 |