PHP單例模式模擬Java Bean實(shí)現(xiàn)方法示例
本文實(shí)例講述了PHP單例模式模擬Java Bean實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
問題:
根據(jù)如下楊輝三角形
實(shí)現(xiàn)一個(gè)get_value($row,$col)
方法:
(前一個(gè)由于代碼是手機(jī)編輯的,很亂,重新發(fā)下)只是為了實(shí)現(xiàn)這個(gè)方法,很簡單,幾行代碼就能實(shí)現(xiàn),但如果行和列的值稍微大點(diǎn),你就發(fā)現(xiàn),運(yùn)行時(shí)間很長。所以就這次的題做了個(gè)稍微復(fù)雜點(diǎn)的例子,說明下單例模式的使用、static的使用、模擬Java Bean、static的使用、遞歸函數(shù)案例等。
/** * author Winter * 2016-11-22 * PHP的單例模式 * 模擬Java Bean * Class Php_bean */ class Php_bean{ private static $_instance = null; private function __construct(){} private $hit = 0;//命中次數(shù) private $array = array();//緩存 private $itratorCount = 0;//迭代次數(shù) public function add_itratorCount(){ $this->itratorCount ++; } public function get_itratorCount(){ return $this->itratorCount; } public function set_cache($row,$col,$value){ $this->array[$row."_".$col] = $value; } public function get_cache($row,$col){ if(isset($this->array[$row."_".$col])){ return $this->array[$row."_".$col]; }else{ return false; } } public function add_hit(){ $this->hit ++; } public function get_hit(){ return $this->hit; } public static function instance(){ if(self::$_instance instanceof self) return self::$_instance; self::$_instance = new self; return self::$_instance; } } /** * @param $row 行 * @param $col 列 * @return int */ function get_value($row,$col){ $php_bean = Php_bean::instance(); $php_bean->add_itratorCount(); if($col > $row) return 0; if($row <=0) return 0; if($col == $row) return 1; if($row == 1) return 1; if($col == 1) return 1; $pre = $php_bean->get_cache($row-1,$col-1); $next = $php_bean->get_cache($row-1,$col-0); if($pre === false){ $pre = get_value($row-1,$col-1); $php_bean->set_cache($row-1,$col-1,$pre); }else{ $php_bean->add_hit(); } if($next === false){ $next = get_value($row-1,$col-0); $php_bean->set_cache($row-1,$col-0,$next); }else{ $php_bean->add_hit(); } $value = $pre + $next; return $value; } $v = get_value(6,6); var_dump($v); $php_bean_obj = Php_bean::instance(); echo "hit:".$php_bean_obj->get_hit()."<br/>"; echo "itratorCount:".$php_bean_obj->get_itratorCount()."<br/>";
運(yùn)行結(jié)果:
int(1) hit:0
itratorCount:1
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP微信開發(fā)之微信錄音臨時(shí)轉(zhuǎn)永久存儲
這篇文章主要為大家詳細(xì)介紹了PHP微信開發(fā)之微信錄音臨時(shí)轉(zhuǎn)永久存儲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01PHP實(shí)現(xiàn)找出數(shù)組中出現(xiàn)次數(shù)超過數(shù)組長度一半的數(shù)字算法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)找出數(shù)組中出現(xiàn)次數(shù)超過數(shù)組長度一半的數(shù)字算法,涉及php數(shù)組的遍歷、統(tǒng)計(jì)、判斷等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10php從右向左/從左向右截取字符串的實(shí)現(xiàn)方法
我先學(xué)的的asp,asp里截取字符串的函數(shù)很簡單,也容易理解:left和right而php里從左向右截取和從右向左截取都是一個(gè)函數(shù):substr2011-11-11php代碼調(diào)試?yán)鱢irephp安裝與使用方法分析
這篇文章主要介紹了php代碼調(diào)試?yán)鱢irephp安裝與使用方法,簡單分析了firephp的下載、安裝、測試代碼調(diào)試等使用方法及相關(guān)操作技巧,需要的朋友可以參考下2018-08-08獲取php頁面執(zhí)行時(shí)間,數(shù)據(jù)庫讀寫次數(shù),函數(shù)調(diào)用次數(shù)等(THINKphp)
這篇文章主要是thinkphp獲取php頁面執(zhí)行時(shí)間,數(shù)據(jù)庫讀寫次數(shù),函數(shù)調(diào)用次數(shù)等,需要的朋友可以參考下2013-06-06