您好,登錄后才能下訂單哦!
這篇文章主要講解了“php代理模式和外觀設(shè)計模式分別是什么”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“php代理模式和外觀設(shè)計模式分別是什么”吧!
Proxy(代理模式)和Facade(外觀)設(shè)計模式 它們均為更復(fù)雜的功能提供抽象化的概念,但這兩種實(shí)現(xiàn)抽象化的過程大不相同
Proxy(代理模式)和Facade(外觀)設(shè)計模式
它們均為更復(fù)雜的功能提供抽象化的概念,但這兩種實(shí)現(xiàn)抽象化的過程大不相同
Proxy案例中,所有的方法和成員變量都來自于目標(biāo)對象,必要時,該代理能夠?qū)λ鶄鬟f的數(shù)據(jù)進(jìn)行修改或檢查
魔術(shù)方法使得Proxy的實(shí)現(xiàn)變的簡單,Proxy模式的一類應(yīng)用時用來記錄方法的訪問信息
還可以利用Proxy的類確定代碼的范圍或調(diào)試程序中存在的問題
<?phpclass LoggingProxy{ private $target; //傳遞進(jìn)去一個對象 public function __construct($target){ $this->target=$target; } protected function log($line){ echo "[".$line."]\r\n"; } public function __set($name,$value){ $this->target->$name=$value; $this->log("setting value for $name:$value "); } public function __get($name){ $value=$this->target->$name; $this->log("Getting value for $name:$value"); return $value; } public function __isset($name){ $value=isset($this->target->$name); $this->log("checking isset for $name".($value?"true":"false")); return $value; } public function __call($name,$arguments){ $this->log("calling method $name width:".implode(",", $arguments)); return call_user_func_array(array($this->target,$name), $arguments); } }class People{ public $name='hk'; public $age; public function sayname(){ return $this->name; } public function plus($a,$b){ return $a+$b; } }$p=new People();$proxy=new LoggingProxy($p);echo $proxy->name;//hkecho " ";$proxy->age=10;echo $p->age;//10echo " ";echo $proxy->sayname();echo " ";echo $proxy->plus(2,3);
結(jié)果
[Getting value for name:hk] hk
[setting value for age:10 ] 10
[calling method sayname width:] hk
[calling method plus width:2,3] 5
多數(shù)情況下Proxy不應(yīng)該改變它所代理的類的行為
Proxy與它所代理的在類型上不完全一致,這也是其一個缺點(diǎn)。
因而若需要進(jìn)行類型提示或代碼檢查以確保對象是某一特定類型,這種情況下就不能使用代理模式三門峽婦科醫(yī)院http://www.smxrlyy.com/
Facade(外觀)模式提供了不同的功能,用來抽象化復(fù)雜功能,以使應(yīng)用程序無需了解子系統(tǒng)處理
各請求的細(xì)節(jié),就能完成整個處理過程
例如:處理典型api請求時,用戶需要通過子系統(tǒng)進(jìn)行認(rèn)證,認(rèn)證通過后,請求通過api子系統(tǒng)傳遞給遠(yuǎn)程服務(wù)器
處理,最后通過其它api的函數(shù)對相應(yīng)解碼
Facade方法粗略實(shí)現(xiàn)如下
<?phpclass Facade{ public function apiRequestJson($method,$parameters){ $user=User::getAuthenticatedUser(); if($user->hasPermission($method)){ $result=$this->api->$method($parameters); return json_encode($result); } } }
Facade并不為子系統(tǒng)添加任何新的功能,而是為子系統(tǒng)委托合適的責(zé)任。
子系統(tǒng)無需知道Facade的存在,而應(yīng)用程序也無需知道子系統(tǒng)的存在。
下面代碼,沒使用外觀模式前
<?phpfunction getProductFileLines($file){ return file($file); }function getProductObjectFromId($id,$productname){ return new Product($id,$productname); }function getNameFromLine($line){ if(preg_match('#.*-(.*)\s\d+#', $line,$array)){ return str_replace('_', ' ', $array[1]); } return ''; }function getIdFromLine($line){ if(preg_match('#^(\d{1,3})-#', $line,$arr)){ return $arr[1]; } return -1; }class Product{ public $id; public $name; public function __construct($id,$name){ $this->id=$id; $this->name=$name; } }$lines=getProductFileLines('t.txt');$objects=array();foreach ($lines as $line){ $id=getIdFromLine($line); $name=getNameFromLine($line); $objects[$id]=getProductObjectFromId($id, $name); }
t.txt內(nèi)容
234-ladies_jumper 55532-gents_hat 44
如果像以上代碼調(diào)用子系統(tǒng),我們的代碼和子系統(tǒng)緊緊耦合在一起,當(dāng)子系統(tǒng)變化時,或者我們決定將其與子系統(tǒng)完全斷開時,代碼就會出問題,所以我們需要在
這些子系統(tǒng)和代碼中引入一個入口。
class ProductFacade{ private $products=array(); public function __construct($file){ $this->file=$file; $this->compile(); } private function compile(){ $lines=getProductFileLines($this->file); foreach ($lines as $line){ $id=getIdFromLine($line); $name=getNameFromLine($line); $this->products[$id]=getProductObjectFromId($id, $name); } } public function getProducts(){ return $this->products; } public function getProduct($id){ return $this->products[$id]; } }$facade=new ProductFacade('t.txt');echo $facade->getProduct(234)->name;
感謝各位的閱讀,以上就是“php代理模式和外觀設(shè)計模式分別是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對php代理模式和外觀設(shè)計模式分別是什么這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。