网站地图    收藏   

主页 > php专栏 > php面向对象 >

php设计模式 建造者模式 与Adapter(适配器模式) -

来源:自学PHP网    时间:2014-11-30 12:29 作者: 阅读:次

[导读] 适配器模式:将一个类的接口转换成客户希望的另外一个接口,使用原本不兼容的而不能在一起工作的那些类可以在一起工作 建造者模式:将一个复杂对象的构建与它的表示分离,使用同样的...

php设计模式 建造者模式 与Adapter(适配器模式)

适配器模式:将一个类的接口转换成客户希望的另外一个接口,使用原本不兼容的而不能在一起工作的那些类可以在一起工作.

建造者模式:将一个复杂对象的构建与它的表示分离,使用同样的构建过程可以创建不同的表示.

  1. /**  
  2. * 适配器模式  
  3. *  
  4. * 将一个类的接口转换成客户希望的另外一个接口,使用原本不兼容的而不能在一起工作的那些类可以在一起工作  
  5. */ 
  6.  
  7. // 这个是原有的类型  
  8. class OldCache  
  9. {  
  10. public function __construct()  
  11. {  
  12. echo "OldCache construct<br/>";  
  13. } 
  14.  
  15. public function store($key,$value)  
  16. {  
  17. echo "OldCache store<br/>";  
  18. } 
  19.  
  20. public function remove($key)  
  21. {  
  22. echo "OldCache remove<br/>";  
  23. } 
  24.  
  25. public function fetch($key)  
  26. {  
  27. echo "OldCache fetch<br/>";  
  28. }  
  29. } 
  30.  
  31. interface Cacheable  
  32. {  
  33. public function set($key,$value);  
  34. public function get($key);  
  35. public function del($key);  
  36. } 
  37.  
  38. class OldCacheAdapter implements Cacheable  
  39. {  
  40. private $_cache = null;  
  41. public function __construct()  
  42. {  
  43. $this->_cache = new OldCache();  
  44. } 
  45.  
  46. public function set($key,$value)  
  47. {  
  48. return $this->_cache->store($key,$value);  
  49. } 
  50.  
  51. public function get($key)  
  52. {  
  53. return $this->_cache->fetch($key);  
  54. } 
  55.  
  56. public function del($key)  
  57. {  
  58. return $this->_cache->remove($key);  
  59. }  
  60. } 
  61.  
  62. $objCache = new OldCacheAdapter();  
  63. $objCache->set("test",1);  
  64. $objCache->get("test");  
  65. $objCache->del("test",1); 

php设计模式 Builder(建造者模式).

  1. /**  
  2. * 建造者模式  
  3. *  
  4. * 将一个复杂对象的构建与它的表示分离,使用同样的构建过程可以创建不同的表示  
  5. */  
  6. class Product  
  7. {  
  8. public $_type = null;  
  9. public $_size = null;  
  10. public $_color = null; 
  11.  
  12. public function setType($type)  
  13. {  
  14. echo "set product type<br/>";  
  15. $this->_type = $type;  
  16. } 
  17.  
  18. public function setSize($size)  
  19. {  
  20. echo "set product size<br/>";  
  21. $this->_size = $size;  
  22. } 
  23.  
  24. public function setColor($color)  
  25. {  
  26. echo "set product color<br/>";  
  27. $this->_color = $color;  
  28. }  
  29. } 
  30.  
  31. $config = array(  
  32. "type"=>"shirt",  
  33. "size"=>"xl",  
  34. "color"=>"red",  
  35. ); 
  36.  
  37. // 没有使用bulider以前的处理  
  38. $oProduct = new Product();  
  39. $oProduct->setType($config['type']);  
  40. $oProduct->setSize($config['size']);  
  41. $oProduct->setColor($config['color']); 
  42.  
  43.  
  44. // 创建一个builder类  
  45. class ProductBuilder  
  46. {  
  47. var $_config = null;  
  48. var $_object = null; 
  49.  
  50. public function ProductBuilder($config)  
  51. {  
  52. $this->_object = new Product();  
  53. $this->_config = $config;  
  54. } 
  55.  
  56. public function build()  
  57. {  
  58. echo "--- in builder---<br/>";  
  59. $this->_object->setType($this->_config['type']);  
  60. $this->_object->setSize($this->_config['size']);  
  61. $this->_object->setColor($this->_config['color']);  
  62. } 
  63.  
  64. public function getProduct()  
  65. {  
  66. return $this->_object;  
  67. }  
  68. } 
  69. $objBuilder = new ProductBuilder($config);  
  70. $objBuilder->build();  
  71. $objProduct = $objBuilder->getProduct(); 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论