[PHP] 代理模式 php设计模式 →→→→→进入此内容的聊天室

来自 , 2019-05-18, 写在 PHP, 查看 105 次.
URL http://www.code666.cn/view/a6db4ed0
  1. /**
  2.  * Subject interface.
  3.  * Client depends only on this abstraction.
  4.  */
  5. interface Image {
  6.     public function getWidth();
  7.  
  8.     public function getHeight();
  9.  
  10.     public function getPath();
  11.  
  12.     /**
  13.      * @return string   the image's byte stream
  14.      */
  15.     public function dump();
  16. }
  17.  
  18. /**
  19.  * Abstract class to avoid repetition of boilerplate code in the Proxy
  20.  * and in the Subject. Only the methods which can be provided without
  21.  * instancing the RealSubject are present here.
  22.  */
  23. abstract class AbstractImage implements Image {
  24.     protected $_width;
  25.     protected $_height;
  26.     protected $_path;
  27.     protected $_data;
  28.  
  29.     public function getWidth() {
  30.         return $this->_width;
  31.     }
  32.  
  33.     public function getHeight() {
  34.         return $this->_height;
  35.     }
  36.  
  37.     public function getPath() {
  38.         return $this->_path;
  39.     }
  40. }
  41.  
  42. /**
  43.  * The RealSubject. Always loads the image, even if no dump of the data
  44.  * is required.
  45.  */
  46. class RawImage extends AbstractImage {
  47.     public function __construct ($path) {
  48.         $this->_path = $path;
  49.         list ($this->_width, $this->_height) = getimagesize ($path);
  50.         $this->_data = file_get_contents ($path);
  51.     }
  52.  
  53.     public function dump() {
  54.         return $this->_data;
  55.     }
  56. }
  57.  
  58. /**
  59.  * Proxy. Defers loading the image data until it becomes really mandatory.
  60.  * This class does its best to postpone the very expensive operations
  61.  * such as the actual loading of the BLOB.
  62.  */
  63. class ImageProxy extends AbstractImage {
  64.     public function __construct ($path) {
  65.         $this->_path = $path;
  66.         list ($this->_width, $this->_height) = getimagesize ($path);
  67.     }
  68.  
  69.     /**
  70.      * Creates a RawImage and exploits its functionalities.
  71.      */
  72.     protected function _lazyLoad() {
  73.         if ($this->_realImage == = null) {
  74.             $this->_realImage = new RawImage ($this->_path);
  75.         }
  76.     }
  77.  
  78.     public function dump() {
  79.         $this->_lazyLoad();
  80.         return $this->_realImage->dump();
  81.     }
  82. }
  83.  
  84. /**
  85.  * Client class that does not use the data dump of the image.
  86.  * Passing blindly a Proxy to this class and to other Clients makes sense
  87.  * as the data would be loaded anyway when Image::dump() is called.
  88.  */
  89. class Client {
  90.     public function tag (Image $img) {
  91.         return ';
  92.           }
  93.           }
  94.  
  95.               $path = ' / home / giorgio / shared / Immagini / kiki.png';
  96.               $client = new Client();
  97.  
  98.               $image = new RawImage($path); // loading of the BLOB takes place
  99.               echo $client->tag($image), "\n";
  100.  
  101.               $proxy = new ImageProxy($path);
  102.               echo $client->tag($proxy), "\n"; // loading does not take place even here

回复 "代理模式 php设计模式"

这儿你可以回复上面这条便签

captcha