1. <?php
  2. /** 
  3.  * @name: 设计模式--建造者模式 
  4.  * ============================================================================ 
  5.  * 意图:
  6.  * 将一个复杂对象的构建和它的表示分离,使得同样的构建过程可以创建不同的表示
  7.  * 适用性:
  8.  * 1、当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。
  9.  * 2、当构造过程必须允许被构造对象有不同的表示时。
  10.  * ============================================================================ 
  11.  * @copyright: http://blog.csdn.net/wkjs 
  12.  * @author: 王康 
  13. */ 
  14. ?>
  15. <?php
  16. /** 
  17.  * @name: 设计模式--建造者模式
  18.  * ============================================================================ 
  19.  * 产品类
  20.  * ============================================================================ 
  21.  * @copyright: http://blog.csdn.net/wkjs 
  22.  * @author: 王康 
  23. */ 
  24. class Product {
  25.     
  26.     /**
  27.      * 产品列表
  28.      *
  29.      * @var array 
  30.      *
  31.      */
  32.     var $parts;
  33.     
  34.     /**
  35.      * 添加产品操作
  36.      *
  37.      * @param object $part 产品信息
  38.      * @return void void
  39.      *
  40.      */
  41.     public function Add($part) {
  42.         $this->parts[]=$part;
  43.     }
  44.     
  45.     /**
  46.      * 打印产品信息
  47.      *
  48.      * @return void void
  49.      *
  50.      */
  51.     public function show() {
  52.         echo '产品列表'.'<br /><hr />';
  53.         foreach($this->parts as $key=>$value) {
  54.             echo $key.'=>'.$value.'<br />';
  55.         }
  56.     }
  57. }
  58. ?>
  59. <?php
  60. /** 
  61.  * @name: 设计模式--建造者模式
  62.  * ============================================================================ 
  63.  * 测试代码 
  64.  * ============================================================================ 
  65.  * @copyright: http://blog.csdn.net/wkjs 
  66.  * @author: 王康 
  67. */ 
  68. /** 
  69.  * @name: 设计模式--建造者模式
  70.  * ============================================================================ 
  71.  * 建造者抽象类
  72.  * ============================================================================ 
  73.  * @copyright: http://blog.csdn.net/wkjs 
  74.  * @author: 王康 
  75. */ 
  76. abstract class Builder {
  77.     
  78.     /**
  79.      * 创建A部分
  80.      *
  81.      * @return void void
  82.      *
  83.      */
  84.     public abstract function BuildPartA();
  85.     
  86.     /**
  87.      * 创建B部分
  88.      *
  89.      * @return void void
  90.      *
  91.      */
  92.     public abstract function BuildPartB();
  93.     
  94.     /**
  95.      * 获取创建结果
  96.      *
  97.      * @return Product 产品结果对象
  98.      *
  99.      */
  100.     public abstract function GetResult();
  101. }
  102. ?>
  103. <?php
  104. /** 
  105.  * @name: 设计模式--建造者模式
  106.  * ============================================================================ 
  107.  * 测试代码 
  108.  * ============================================================================ 
  109.  * @copyright: http://blog.csdn.net/wkjs 
  110.  * @author: 王康 
  111. */ 
  112. class ConcreteBuilder1 extends Builder {
  113.     
  114.     /**
  115.      * 产品列标
  116.      *
  117.      * @var Product 
  118.      *
  119.      */
  120.     var $product;
  121.     
  122.     /**
  123.      * 构造函数
  124.      *
  125.      * @return void void
  126.      *
  127.      */
  128.     function ConcreteBuilder1() {
  129.         $this->product=new Product();
  130.     }
  131.     
  132.     public function BuildPartA() {
  133.         $this->product->Add('PartA');
  134.     }
  135.     
  136.     public function BuildPartB() {
  137.         $this->product->Add('PartB');
  138.     }
  139.     
  140.     public function GetResult() {
  141.         return $this->product;
  142.     }
  143. }
  144. ?>
  145. <?php
  146. /** 
  147.  * @name: 设计模式--建造者模式
  148.  * ============================================================================ 
  149.  * 实际类一
  150.  * ============================================================================ 
  151.  * @copyright: http://blog.csdn.net/wkjs 
  152.  * @author: 王康 
  153. */ 
  154. class ConcreteBuilder2 extends Builder {
  155.     
  156.     /**
  157.      * 产品列标
  158.      *
  159.      * @var Product 
  160.      *
  161.      */
  162.     var $product;
  163.     
  164.     /**
  165.      * 构造函数
  166.      *
  167.      * @return void void
  168.      *
  169.      */
  170.     function ConcreteBuilder2() {
  171.         $this->product=new Product();
  172.     }
  173.     
  174.     public function BuildPartA() {
  175.         $this->product->Add('PartX');
  176.     }
  177.     
  178.     public function BuildPartB() {
  179.         $this->product->Add('PartY');
  180.     }
  181.     
  182.     public function GetResult() {
  183.         return $this->product;
  184.     }
  185. }
  186. ?>
  187. <?php
  188. /** 
  189.  * @name: 设计模式--建造者模式
  190.  * ============================================================================ 
  191.  * 实际类二
  192.  * ============================================================================ 
  193.  * @copyright: http://blog.csdn.net/wkjs 
  194.  * @author: 王康 
  195. */ 
  196. class Director {
  197.     
  198.     /**
  199.      * 创建类
  200.      *
  201.      * @param Builder $builder 创建实体
  202.      * @return void void
  203.      *
  204.      */
  205.     public function Construct($builder) {
  206.         $builder->BuildPartA();
  207.         $builder->BuildPartB();
  208.     }
  209. }
  210. ?>
  211. <?php
  212. /** 
  213.  * @name: 设计模式--建造者模式
  214.  * ============================================================================ 
  215.  * 测试代码 
  216.  * ============================================================================ 
  217.  * @copyright: http://blog.csdn.net/wkjs 
  218.  * @author: 王康 
  219. */ 
  220. include_once 'class.Builder.php';
  221. $director=new Director();
  222. $b1=new ConcreteBuilder1();
  223. $b2=new ConcreteBuilder2();
  224. $director->Construct($b1);
  225. $p1=$b1->GetResult();
  226. $p1->show();
  227. $director->Construct($b2);
  228. $p2=$b2->GetResult();
  229. $p2->show();
  230. ?>
posted on 2008-10-15 15:42  wkjs  阅读(150)  评论(0编辑  收藏  举报