您好,登錄后才能下訂單哦!
這篇文章主要講解了“PHP中的代理模式詳細(xì)介紹”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“PHP中的代理模式詳細(xì)介紹”吧!
代理模式(Proxy),它是對簡單處理程序(或指針)的增強(qiáng),用于引用一個對象:這個指針被代理(Proxy)對象取代,代理對象位于客戶端(Client)和真實(shí)執(zhí)行程序之間,指針有一個可被多個目標(biāo)利用的鉤子。
從技術(shù)上講,這種模式在客戶端和真實(shí)主體(RealSubject)之間插入一個代理對象,維護(hù)subject接口和用不同的方式委派它的方法。代理可以透明地做任何事情:懶散創(chuàng)建RealSubject或載入數(shù)據(jù),與其它機(jī)器交換消息,寫時復(fù)制策略等。這與HTTP代理有點(diǎn)類似,其客戶端(如瀏覽器)和應(yīng)用程序依賴于與HTTP服務(wù)器的聯(lián)系,代理在管理連接時可以完成其它任務(wù),如訪問控制和緩存大型下載文件。
代理模式的對象圖與裝飾模式對象圖在結(jié)構(gòu)上類似,但表達(dá)的目的各有不同,裝飾者給對象動態(tài)增加行為,而代理則控制來自客戶端的訪問。此外,代理只在需要時才創(chuàng)建RealSubject。
參與者:
◆客戶端(Client):取決于主體(Subject)實(shí)現(xiàn);
◆主體(Subject):RealSubject的抽象;
◆真實(shí)主體(RealSubject):完成代價高昂的工作或包含大量的數(shù)據(jù);
◆代理(Proxy):為Client提供一個與Subject一致的引用,僅在需要時才創(chuàng)建RealSubject實(shí)例或與RealSubject實(shí)例通信。
下面是兩個被廣泛使用的代理模式例子:
1、對象-關(guān)系映射(Orms)在運(yùn)行中創(chuàng)建代理作為實(shí)體類的子類,以實(shí)現(xiàn)懶散加載(虛擬代理),這個代理會覆蓋所有實(shí)體方法,在前面追加一個載入程序,在方法被真正調(diào)用前不會包含任何數(shù)據(jù),Orms代理支持對象間的雙向關(guān)系,不用加載整個數(shù)據(jù)庫,因?yàn)樗鼈儽恢糜诋?dāng)前加載對象圖的邊界。
2、Java RMI使用遠(yuǎn)程代理對象(遠(yuǎn)程代理),當(dāng)它們的方法被調(diào)用時,代理序列化參數(shù),執(zhí)行網(wǎng)絡(luò)上的請求,委托調(diào)用另一個節(jié)點(diǎn)上的真實(shí)對象,這種技術(shù)允許透明地調(diào)用遠(yuǎn)程對象,不用擔(dān)心它們是否在同一臺機(jī)器上,但這種透明度很容易會使執(zhí)行速度變慢。
下面的代碼示例實(shí)現(xiàn)了一個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
感謝各位的閱讀,以上就是“PHP中的代理模式詳細(xì)介紹”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對PHP中的代理模式詳細(xì)介紹這一問題有了更深刻的體會,具體使用情況還需要大家實(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)容。