中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

PHP設(shè)計(jì)模式之代理模式的深入解析

代理模式(Proxy),它是對(duì)簡(jiǎn)單處理程序(或指針)的增強(qiáng),用于引用一個(gè)對(duì)象:這個(gè)指針被代理(Proxy)對(duì)象取代,代理對(duì)象位于客戶端(Client)和真實(shí)執(zhí)行程序之間,指針有一個(gè)可被多個(gè)目標(biāo)利用的鉤子。

從技術(shù)上講,這種模式在客戶端和真實(shí)主體(RealSubject)之間插入一個(gè)代理對(duì)象,維護(hù)subject接口和用不同的方式委派它的方法。代理可以透明地做任何事情:懶散創(chuàng)建RealSubject或載入數(shù)據(jù),與其它機(jī)器交換消息,寫(xiě)時(shí)復(fù)制策略等。這與HTTP代理有點(diǎn)類(lèi)似,其客戶端(如瀏覽器)和應(yīng)用程序依賴(lài)于與HTTP服務(wù)器的聯(lián)系,代理在管理連接時(shí)可以完成其它任務(wù),如訪問(wèn)控制和緩存大型下載文件。


代理模式的對(duì)象圖與裝飾模式對(duì)象圖在結(jié)構(gòu)上類(lèi)似,但表達(dá)的目的各有不同,裝飾者給對(duì)象動(dòng)態(tài)增加行為,而代理則控制來(lái)自客戶端的訪問(wèn)。此外,代理只在需要時(shí)才創(chuàng)建RealSubject。

參與者:
◆客戶端(Client):取決于主體(Subject)實(shí)現(xiàn);
◆主體(Subject):RealSubject的抽象;
◆真實(shí)主體(RealSubject):完成代價(jià)高昂的工作或包含大量的數(shù)據(jù);
◆代理(Proxy):為Client提供一個(gè)與Subject一致的引用,僅在需要時(shí)才創(chuàng)建RealSubject實(shí)例或與RealSubject實(shí)例通信。

下面是兩個(gè)被廣泛使用的代理模式例子:
1、對(duì)象-關(guān)系映射(Orms)在運(yùn)行中創(chuàng)建代理作為實(shí)體類(lèi)的子類(lèi),以實(shí)現(xiàn)懶散加載(虛擬代理),這個(gè)代理會(huì)覆蓋所有實(shí)體方法,在前面追加一個(gè)載入程序,在方法被真正調(diào)用前不會(huì)包含任何數(shù)據(jù),Orms代理支持對(duì)象間的雙向關(guān)系,不用加載整個(gè)數(shù)據(jù)庫(kù),因?yàn)樗鼈儽恢糜诋?dāng)前加載對(duì)象圖的邊界。

2、Java RMI使用遠(yuǎn)程代理對(duì)象(遠(yuǎn)程代理),當(dāng)它們的方法被調(diào)用時(shí),代理序列化參數(shù),執(zhí)行網(wǎng)絡(luò)上的請(qǐng)求,委托調(diào)用另一個(gè)節(jié)點(diǎn)上的真實(shí)對(duì)象,這種技術(shù)允許透明地調(diào)用遠(yuǎn)程對(duì)象,不用擔(dān)心它們是否在同一臺(tái)機(jī)器上,但這種透明度很容易會(huì)使執(zhí)行速度變慢。

下面的代碼示例實(shí)現(xiàn)了一個(gè)ImageProxy,推遲了圖像數(shù)據(jù)的加載。
復(fù)制代碼 代碼如下:
/** 
 * Subject interface. 
 * Client depends only on this abstraction. 
 */
interface Image 

    public function getWidth(); 

    public function getHeight(); 

    public function getPath(); 

    /** 
     * @return string   the image's byte stream 
     */
    public function dump(); 


/** 
 * Abstract class to avoid repetition of boilerplate code in the Proxy 
 * and in the Subject. Only the methods which can be provided without 
 * instancing the RealSubject are present here. 
 */
abstract class AbstractImage implements Image 

    protected $_width; 
    protected $_height; 
    protected $_path; 
    protected $_data; 

    public function getWidth() 
    { 
 return $this->_width; 
    } 

    public function getHeight() 
    { 
 return $this->_height; 
    } 

    public function getPath() 
    { 
 return $this->_path; 
    } 


/** 
 * The RealSubject. Always loads the image, even if no dump of the data 
 * is required. 
 */
class RawImage extends AbstractImage 

    public function __construct($path) 
    { 
 $this->_path = $path; 
 list ($this->_width, $this->_height) = getimagesize($path); 
 $this->_data = file_get_contents($path); 
    } 

    public function dump() 
    { 
 return $this->_data; 
    } 


/** 
 * Proxy. Defers loading the image data until it becomes really mandatory. 
 * This class does its best to postpone the very expensive operations 
 * such as the actual loading of the BLOB. 
 */
class ImageProxy extends AbstractImage 

    public function __construct($path) 
    { 
 $this->_path = $path; 
 list ($this->_width, $this->_height) = getimagesize($path); 
    } 

    /** 
     * Creates a RawImage and exploits its functionalities. 
     */
    protected function _lazyLoad() 
    { 
 if ($this->_realImage === null) { 
     $this->_realImage = new RawImage($this->_path); 
 } 
    } 

    public function dump() 
    { 
 $this->_lazyLoad(); 
 return $this->_realImage->dump(); 
    } 


/** 
 * Client class that does not use the data dump of the image. 
 * Passing blindly a Proxy to this class and to other Clients makes sense 
 * as the data would be loaded anyway when Image::dump() is called. 
 */
class Client 

    public function tag(Image $img) 
    { 
 return '; 
    } 


$path = '/home/giorgio/shared/Immagini/kiki.png'; 
$client = new Client(); 

$image = new RawImage($path); // loading of the BLOB takes place 
echo $client->tag($image), "/n"; 

$proxy = new ImageProxy($path); 
echo $client->tag($proxy), "/n"; // loading does not take place even here

以上代碼實(shí)現(xiàn)了php的代理模式。簡(jiǎn)單來(lái)講,代理模式就是為其他對(duì)象提供一個(gè)代理以控制對(duì)這個(gè)對(duì)象的訪問(wèn)。

php技術(shù)PHP設(shè)計(jì)模式之代理模式的深入解析,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 日韩欧美三区 | 国产在线资源 | 国产69久久精品成人看动漫 | 国产大片黄色 | 亚洲欧美日韩精品久久亚洲区 | 香蕉久久久 | 国产精品永久免费 | 麻豆a级片| 日韩中文字幕2019 | 精品乱人伦一区二区三区 | 91色网站| 久久香蕉精品视频 | 久久精品在线 | 久久久网| 午夜爽爽爽男女免费观看影院 | 中文字幕精品一区二区三区精品 | 91在线视频在线观看 | 国产乱码精品一区二区三区五月婷 | 亚洲精品日韩视频 | 日韩福利 | 国产精品96久久久久久 | 四虎影院一区二区 | 久久久久香蕉视频 | 爱操影视 | 精品欧美一区二区久久久伦 | 国产黄色大片在线免费观看 | 久久999| 日韩高清在线 | 91中文字幕在线观看 | 欧美精品在线播放 | 国内精品免费久久久久软件老师 | 九九久久久久久 | 精品1区 | 天天干,夜夜操 | 国产伦精品一区二区三区照片91 | 国产精品无码久久久久 | 久久机热 | 国产蜜臀97一区二区三区 | 自拍偷拍亚洲欧美 | 日韩一区在线视频 | 中文字字幕一区二区三区四区五区 |