[设计模式之禅读书笔记]011_23种设计模式五:建造者模式

   序言

   建造者模式,这是一个无法从字面上推测出类图的模式,但是有了前面的模板方法模式的基础,在理解建造者模式上,会觉得很简单。我也尽量多用图来解释建造者模式,因为今天在看别人的文章的时候,我发现自己很懒,懒得看那一堆堆的文字。文字固然可以灵巧的组成散文诗歌,但是对于时间仓促,浮躁的程序员来说,如果不是需要这门技术,自己根本没闲心去看别人的文字。唉,序言有点罗嗦了,估计80%的人都不会完整的看完序言,不过没关系,看正文就行。

   正文

   1. 在脑子里形成一个需求图

       

   稍微解释一下这个图,这个图定义了一个产品的模板,姑且叫产品这个名字吧!其中“执行”这个行为是“操作1”、“操作2”、“操作3”这三个操作的组合,不过,执行顺序是不定的,如何构造一个执行序列不一定的方法呢?我们可以靠数组来定义这个执行序列,参考下列代码:

 1     void setSequence(vector<string> quen){
 2         this->sequen = quen;
 3     }
 4     void execute(){
 5         for(int i = 0; i < sequen.size(); i ++){
 6             if(sequen[i].compare("operator1") == 0){
 7                 this->operator1();
 8             }else if(sequen[i].compare("operator2") == 0){
 9                 this->operator2();
10             }else if(sequen[i].compare("operator3") == 0){
11                 this->operator3();
12             }
13         }
14     }

   2. 同一产品系列产品执行序列不同到底是何意?

       

   这是一个扩充的需求图,不过,我特意标识的红字部分只有dota玩家能懂。不过没关系,我来解释一下,这里我们看到产品系列1和产品系列2中的操作是不同的,但是仍然符合产品模板的规定。其次,两个产品系列下,都有各自的产品。可以看出,在各个产品中,操作执行序列是不同的。也就是说,操作序列是最后定义的。

   现在抛出一个问题,上面的模型该怎么设计呢?由于这节学习的建造者模式,所以,下面我们来一步一步来实现这个建造者模式。

   3. 产品模板

   产品模板很简单,定义三个操作和一个执行序列就可以了,但是我们还需要一个可以设置执行序列的数组,这个数组由一个方法来设置。代码如下:

 1 class AbstractProduct{
 2 public:
 3     virtual void operator1(){}
 4     virtual void operator2(){}
 5     virtual void operator3(){}
 6     void setSequence(vector<string> quen){
 7         this->sequen = quen;
 8     }
 9     void execute(){
10         for(int i = 0; i < sequen.size(); i ++){
11             if(sequen[i].compare("operator1") == 0){
12                 this->operator1();
13             }else if(sequen[i].compare("operator2") == 0){
14                 this->operator2();
15             }else if(sequen[i].compare("operator3") == 0){
16                 this->operator3();
17             }
18         }
19     }
20 private:
21     vector<string> sequen;
22 };

   聪明的小孩会看到,这不就是模板方法模式吗?把具体实现延迟到子类,父类定义好算法的框架,是的,没错,这就是模板方法模式。

   2. 产品系列的实现

   我们有两个产品系列,其中都实现了产品模板中没有实现的三个操作,那么两个产品系列是如何的呢?代码如下:

 1 class ConcreteProduct1:public AbstractProduct{
 2     void operator1(){ cout<<"Product1:  operator1 executing."<<endl; }
 3     void operator2(){ cout<<"Product1:  operator2 executing."<<endl; }
 4     void operator3(){ cout<<"Product1:  operator3 executing."<<endl; }
 5 };
 6 
 7 class ConcreteProduct2:public AbstractProduct{
 8     void operator1(){ cout<<"Product2:  operator1 executing."<<endl; }
 9     void operator2(){ cout<<"Product2:  operator2 executing."<<endl; }
10     void operator3(){ cout<<"Product2:  operator3 executing."<<endl; }
11 };

   3. 具体产品的简单构建(从这一点可以看出我们需要建造者模式)

   现在产品系列出来了,如何建立我们需要的产品呢?一般来说,你肯定会如下这么写:

 1 int main(){
 2 
 3     AbstractProduct* product = new ConcreteProduct1();
 4     vector<string> sequen;
 5     sequen.push_back("operator3");
 6     sequen.push_back("operator2");
 7     sequen.push_back("operator1");
 8     product->setSequence(sequen);
 9     product->execute();
10 
11     system("pause");
12     return 0;
13 }

   这样写是可以得到我们需要的结果,但是,试想如果我需要很多个不同操作序列的产品,那我这个main里面是不是会很庞大?那该怎么改进呢?

   4. 我们需要建造者

   建造者长什么样呢?至少能不用让我们自己一直new产品吧!代码参考如下:

 1 class ConcreteBuilder1:public AbstractBuilder{
 2 public:
 3     AbstractProduct *getProduct(){
 4         return product;
 5     }
 6     void setSequence(vector<string> quen){
 7         product = new ConcreteProduct1();
 8         product->setSequence(quen);
 9     }
10 private:
11     ConcreteProduct1* product;
12 };
13 
14 class ConcreteBuilder2:public AbstractBuilder{
15 public:
16     AbstractProduct *getProduct(){
17         return product;
18     }
19     void setSequence(vector<string> quen){
20         product = new ConcreteProduct2();
21         product->setSequence(quen);
22     }
23 private:
24     ConcreteProduct2* product;
25 };

   上面是两个产品系列的建造者,这个时候,我们的客户端调用就变成下面这个样子了:

 1 int main(){
 2 
 3     AbstractBuilder* builder = new ConcreteBuilder1();
 4     vector<string> sequen;
 5     sequen.push_back("operator3");
 6     sequen.push_back("operator2");
 7     sequen.push_back("operator1");
 8     builder->setSequence(sequen);
 9     AbstractProduct* product = builder->getProduct();
10     product->execute();
11 
12     system("pause");
13     return 0;
14 }

   看出来不同没?我们有Builder了,只要new一个Builder出来,下面我们就不用自己new各个产品了。那么这样的代码看起来还是不够明晰,main里面,即客户端调用的代码看起来还是不清爽,这时候我们需要引进一个新类——Director。

   5. 隆重登场Director(封装了各种产品)

   它的职责其实就是将上面main里面出现的那一堆代码封装起来,然后提供一些方法来构建我们的产品,代码参考如下:

 1 class Director{
 2 public:
 3     AbstractProduct* getConcreteProduct1_1_2_3(){
 4         AbstractBuilder* builder = new ConcreteBuilder1();
 5         vector<string> sequen;
 6         sequen.push_back("operator1");
 7         sequen.push_back("operator2");
 8         sequen.push_back("operator3");
 9         builder->setSequence(sequen);
10         AbstractProduct* product = builder->getProduct();
11         return product;
12     }
13 
14     AbstractProduct* getConcreteProduct1_3_2_1(){
15         AbstractBuilder* builder = new ConcreteBuilder1();
16         vector<string> sequen;
17         sequen.push_back("operator3");
18         sequen.push_back("operator2");
19         sequen.push_back("operator1");
20         builder->setSequence(sequen);
21         AbstractProduct* product = builder->getProduct();
22         return product;
23     }
24 
25     AbstractProduct* getConcreteProduct2_1_2_3(){
26         AbstractBuilder* builder = new ConcreteBuilder2();
27         vector<string> sequen;
28         sequen.push_back("operator1");
29         sequen.push_back("operator2");
30         sequen.push_back("operator3");
31         builder->setSequence(sequen);
32         AbstractProduct* product = builder->getProduct();
33         return product;
34     }
35 
36     AbstractProduct* getConcreteProduct2_3_2_1(){
37         AbstractBuilder* builder = new ConcreteBuilder2();
38         vector<string> sequen;
39         sequen.push_back("operator3");
40         sequen.push_back("operator2");
41         sequen.push_back("operato1");
42         builder->setSequence(sequen);
43         AbstractProduct* product = builder->getProduct();
44         return product;
45     }
46 };

   6. 新的客户端调用(变得很简洁了)

   引进了Director后,再来看看我们的客户端调用吧:

 1 int main(){
 2 
 3     Director* director = new Director();
 4     AbstractProduct* product1 = director->getConcreteProduct1_1_2_3();
 5     product1->execute();
 6 
 7     AbstractProduct* product2 = director->getConcreteProduct1_3_2_1();
 8     product2->execute();
 9 
10     system("pause");
11     return 0;
12 }

   是不是很简洁了,这就是我们最终的结果!!!

   UML图

      

   我偷懒了,没有把方法放到里面,注意上面的红框内的组合,是一个模板方法模式,其他的咱就不多说了,自己研究研究吧!

   总结

   对于建造者模式的理解还仅限于代码而已,实际应用场景需要强化,我尽量找机会时间自己的建造者模式,呵呵!

 

PS:附上最终的完整代码:

  1 #include <iostream>
  2 #include <vector>
  3 using namespace std;
  4 
  5 class AbstractProduct{
  6 public:
  7     virtual void operator1(){}
  8     virtual void operator2(){}
  9     virtual void operator3(){}
 10     void setSequence(vector<string> quen){
 11         this->sequen = quen;
 12     }
 13     void execute(){
 14         for(int i = 0; i < sequen.size(); i ++){
 15             if(sequen[i].compare("operator1") == 0){
 16                 this->operator1();
 17             }else if(sequen[i].compare("operator2") == 0){
 18                 this->operator2();
 19             }else if(sequen[i].compare("operator3") == 0){
 20                 this->operator3();
 21             }
 22         }
 23     }
 24 private:
 25     vector<string> sequen;
 26 };
 27 
 28 class ConcreteProduct1:public AbstractProduct{
 29     void operator1(){ cout<<"Product1:  operator1 executing."<<endl; }
 30     void operator2(){ cout<<"Product1:  operator2 executing."<<endl; }
 31     void operator3(){ cout<<"Product1:  operator3 executing."<<endl; }
 32 };
 33 
 34 class ConcreteProduct2:public AbstractProduct{
 35     void operator1(){ cout<<"Product2:  operator1 executing."<<endl; }
 36     void operator2(){ cout<<"Product2:  operator2 executing."<<endl; }
 37     void operator3(){ cout<<"Product2:  operator3 executing."<<endl; }
 38 };
 39 
 40 class AbstractBuilder{
 41 public:
 42     virtual AbstractProduct *getProduct(){ return NULL;}
 43     virtual void setSequence(vector<string> quen){}
 44 };
 45 
 46 class ConcreteBuilder1:public AbstractBuilder{
 47 public:
 48     AbstractProduct *getProduct(){
 49         return product;
 50     }
 51     void setSequence(vector<string> quen){
 52         product = new ConcreteProduct1();
 53         product->setSequence(quen);
 54     }
 55 private:
 56     ConcreteProduct1* product;
 57 };
 58 
 59 class ConcreteBuilder2:public AbstractBuilder{
 60 public:
 61     AbstractProduct *getProduct(){
 62         return product;
 63     }
 64     void setSequence(vector<string> quen){
 65         product = new ConcreteProduct2();
 66         product->setSequence(quen);
 67     }
 68 private:
 69     ConcreteProduct2* product;
 70 };
 71 
 72 class Director{
 73 public:
 74     AbstractProduct* getConcreteProduct1_1_2_3(){
 75         AbstractBuilder* builder = new ConcreteBuilder1();
 76         vector<string> sequen;
 77         sequen.push_back("operator1");
 78         sequen.push_back("operator2");
 79         sequen.push_back("operator3");
 80         builder->setSequence(sequen);
 81         AbstractProduct* product = builder->getProduct();
 82         return product;
 83     }
 84 
 85     AbstractProduct* getConcreteProduct1_3_2_1(){
 86         AbstractBuilder* builder = new ConcreteBuilder1();
 87         vector<string> sequen;
 88         sequen.push_back("operator3");
 89         sequen.push_back("operator2");
 90         sequen.push_back("operator1");
 91         builder->setSequence(sequen);
 92         AbstractProduct* product = builder->getProduct();
 93         return product;
 94     }
 95 
 96     AbstractProduct* getConcreteProduct2_1_2_3(){
 97         AbstractBuilder* builder = new ConcreteBuilder2();
 98         vector<string> sequen;
 99         sequen.push_back("operator1");
100         sequen.push_back("operator2");
101         sequen.push_back("operator3");
102         builder->setSequence(sequen);
103         AbstractProduct* product = builder->getProduct();
104         return product;
105     }
106 
107     AbstractProduct* getConcreteProduct2_3_2_1(){
108         AbstractBuilder* builder = new ConcreteBuilder2();
109         vector<string> sequen;
110         sequen.push_back("operator3");
111         sequen.push_back("operator2");
112         sequen.push_back("operato1");
113         builder->setSequence(sequen);
114         AbstractProduct* product = builder->getProduct();
115         return product;
116     }
117 };
118 int main(){
119 
120     Director* director = new Director();
121     AbstractProduct* product1 = director->getConcreteProduct1_1_2_3();
122     product1->execute();
123 
124     AbstractProduct* product2 = director->getConcreteProduct1_3_2_1();
125     product2->execute();
126 
127     system("pause");
128     return 0;
129 }
posted @ 2012-11-01 00:35  邵贤军  阅读(1705)  评论(7编辑  收藏  举报