首页 > 其他 > 详细

设计模式之五:BUILDER(生成器)—对象创建型模式

时间:2014-03-23 13:42:41      阅读:481      评论:0      收藏:0      [点我收藏+]

2014-03-16 星期日 15:26:39 bubuko.com,布布扣bubuko.com,布布扣

Builder,继续GOF。

1、Intent

?Separate the construction of a complex object from its representation so that the same construction process can create different representations.

将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

2、Also Known As
3、Motivation

?A reader for the RTF (Rich Text Format) document exchange format should be able to convert RTF to many text formats. The reader might convert RTF documents into plain ASCII text or into a text widget that can be edited interactively. The problem, however, is that the number of possible conversions is open-ended. So it should be easy to add a new conversion without modifying the reader.

?一个RTF(Rich Text Format)文档交换格式的阅读器应能将 RTF转换为多种正文格式。该阅读器可以将 RTF文档转换成普通 ASCII文本或转换成一个能以交互方式编辑的正文窗口组件。但问题在于可能转换的数目是无限的。因此要能够很容易实现新的转换的增加,同时却不改变RTF阅读器。

bubuko.com,布布扣

 

4、Applicability

Use the Builder pattern when

?●  the algorithm for creating a complex object should be independent of the parts that make up the object and how they‘re assembled.

?●  the construction process must allow different representations for the object that‘s constructed.

在以下情况使用B u i l d e r模式

  当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。

  当构造过程必须允许被构造的对象有不同的表示时。

5、Structure

bubuko.com,布布扣

bubuko.com,布布扣

6、代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#if 1
    class Product1
    {
    };
 
    class Product1_Part //上面的RTF的part也可以独立使用
    {
    };
 
    class Product2
    {
    };
     
    // class Builder
    class Builder //抽象基类
    {
    public:
        virtual void BuilderPartA() {} //提供缺省实现
        virtual void BuilderPartB() {}
        virtual void BuilderPartC() {}
 
    protected:
        Builder() {}
    };
     
    // class ConcreteBuilder1
    class ConcreteBuilder1 : public Builder //创建Product1
    {
    public:
        ConcreteBuilder1() : _product(NULL) {}
        virtual void BuilderPartA() {}
        virtual void BuilderPartB() {}
        virtual void BuilderPartC() {}
        virtual Product1* GetProduct1() { return _product ; } //返回创建的Product1对象
 
    private:
        Product1 *_product ;
    };
     
    // class ConcreteBuilder2
    class ConcreteBuilder2 : public Builder //创建Product2
    {
    public:
        ConcreteBuilder2() : _product(NULL) {}
        virtual void BuilderPartA() {}
        virtual void BuilderPartB() {}
        virtual void BuilderPartC() {}
        virtual Product2* GetProduct2() { return _product ; } //返回创建的Product2对象
 
    private:
        Product2 *_product ;
    };
     
    // class Director
    class Director
    {
    public:
        //创建对象(Director并不知道具体创建出来的对象是什么样的,只有调用该函数的client知道)
        void Construct(Builder *builder)
        {
            builder->BuilderPartA() ;
            builder->BuilderPartB() ;
            builder->BuilderPartC() ;
        }
    };
 
    //客户端代码:
    void Builder_Demo()
    {
        Director director ;
        // 创建第一种对象
        ConcreteBuilder1 *pBuilder1 = new ConcreteBuilder1();
        director.Construct(pBuilder1);
        Product1 *product1 = pBuilder1->GetProduct1();
 
        //Product1_Part *Product1_Part = pBuilder1->BuilderPartA();
        //partx 独立使用
        pBuilder1->BuilderPartA();
         
        // 创建第二种对象
        ConcreteBuilder2 *pBuilder2 = new ConcreteBuilder2();
        director.Construct(pBuilder2);
        Product2 *product2 = pBuilder2->GetProduct2();
    }
#endif

 

设计模式之五:BUILDER(生成器)—对象创建型模式,布布扣,bubuko.com

设计模式之五:BUILDER(生成器)—对象创建型模式

原文:http://www.cnblogs.com/freezlz/p/3618817.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!