天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

PHP設計模式之代理模式的深入解析

代理模式(Proxy),它是對簡單處理程序(或指針)的增強,用于引用一個對象:這個指針被代理(Proxy)對象取代,代理對象位于客戶端(Client)和真實執行程序之間,指針有一個可被多個目標利用的鉤子。

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


代理模式的對象圖與裝飾模式對象圖在結構上類似,但表達的目的各有不同,裝飾者給對象動態增加行為,而代理則控制來自客戶端的訪問。此外,代理只在需要時才創建RealSubject。

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

下面是兩個被廣泛使用的代理模式例子:
1、對象-關系映射(Orms)在運行中創建代理作為實體類的子類,以實現懶散加載(虛擬代理),這個代理會覆蓋所有實體方法,在前面追加一個載入程序,在方法被真正調用前不會包含任何數據,Orms代理支持對象間的雙向關系,不用加載整個數據庫,因為它們被置于當前加載對象圖的邊界。

2、Java RMI使用遠程代理對象(遠程代理),當它們的方法被調用時,代理序列化參數,執行網絡上的請求,委托調用另一個節點上的真實對象,這種技術允許透明地調用遠程對象,不用擔心它們是否在同一臺機器上,但這種透明度很容易會使執行速度變慢。

下面的代碼示例實現了一個ImageProxy,推遲了圖像數據的加載。
復制代碼 代碼如下:
/** 
 * 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

以上代碼實現了php的代理模式。簡單來講,代理模式就是為其他對象提供一個代理以控制對這個對象的訪問。

php技術PHP設計模式之代理模式的深入解析,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 亚洲精品一本之道高清乱码 | 精品国产国产精2020久久日 | 我在厨房摸岳的乳HD在线观看 | 超碰97av 在线人人操 | 伊人情人网综合 | 在教室伦流澡到高潮HNP视频 | 成人免费看片又大又黄 | 丰满大屁俄罗斯肥女 | 2020亚洲 欧美 国产 日韩 | 纯肉高H放荡受BL文库 | 国内精品九九视频 | 97精品国产亚洲AV高清 | 可以看的黄页的网站 | 国产亚洲欧洲日韩在线三区 | 青青青草免费 | 亚洲字幕久久 | 精品国产在线手机在线 | 狠狠色狠狠色88综合日日91 | 国产强奷糟蹋漂亮邻居在线观看 | 国产3级在线 | 《乳色吐息》无删减版在线观看 | 5580免费午夜福利院 | 嗯啊快拔出来我是你老师视频 | 97在线观看成人免费视频 | 麻豆免费观看高清完整视频 | 99视频在线观看免费视频 | 色妞色视频一区二区三区四区 | 久久精品视频16 | 久久综合狠狠综合狠狠 | 小小水蜜桃3视频在线观看 小向美奈子厨房magnet | 舔1V1高H糙汉 | 热巴两次用约老师屁股发底线球 | 入禽太深在线观看免费高清 | 国产精品99re6热在线播放 | 国产乱码二卡3卡四卡 | 迈开腿让我看下你的小草莓声音 | 友田真希息与子中文字幕 | 果冻传媒独家原创在线观看 | 亚洲bt区 | 猛烈抽插H1V1 | 好大好硬好爽好深好硬视频 |