潇's profile老肖的地盘PhotosBlogListsMore Tools Help

Blog


    October 21

    Builder

    老肖的脑残实现如下:

    class Product
    {

    };

    class Builder
    {
    public:
        virtual Product* BuildPart();
    };

    class ConcreteBuilder1 : Builder
    {
    public:
        Product* BuildPart() { return new Product(); }
        void GetResult();
    };

    class ConcreteBuilder2 : Builder
    {
    public:
        Product* BuildPart() { return new Product(); }
        void GetResult();
    };

    class Director
    {
    private:
        Builder** builders;
    public:
        void Construct()
        {
            builders = new Builder*[2];
            builders[0] = new ConcreteBuilder1();
            builders[1] = new ConcreteBuilder2();

            for(int i=0;i<2;i++)
            {
                builders[i]->BuildPart();
            }
        }
    };

    很显然,我觉得书上的类图和书上的代码实现有出入,既然是Builder对象的聚合,那么为何只作为Director的构造函数的参数进行传入。如果完全按照类图的实现,就像上面的代码那样脑残,在一个循环创建后,根本无法保留创建后的状态,难道类图中隐藏了Dirctor对于Product的聚合?抑或是根据每个Builder对象进行操作,但每个Builder对象对于Product只是个弱引用而已,不解!

    --------------------过了一天,我是分割线-------------------------------------------------------------

    书上的图,很令我迷茫,但是在今天和同学K讨论后又似乎有那么点开窍。

    首先,关于聚合的概念需要进一步更新,聚合意味着一个对象拥有另一个对象或对另一个对象负责。并且聚合对象和其拥有者具有相同的生命周期。于是乎写出如下代码:

    老肖的非脑残实现如下:

    class Product
    {

    };

    class Builder
    {
    public:
        virtual void BuildPart();
        virtual Product* GetResult() { return 0; }
    };

    class ConcreteBuilder : Builder
    {
    private:
        Product* currentProduct;
    public:
        void BuildPart() { currentProduct = new Product(); }
        Product* GetResult() { return currentProduct; }
    };

    class Director
    {
    private:
        Builder* builders;
    public:
        Director(Builder* outBuilder)
        {
            builders = outBuilder;
        }
        void Construct()
        {
            builders->BuildPart();
        }
    };

    适用性:

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

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

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://seanxiaoxiao.spaces.live.com/blog/cns!7677365E5B9342DE!637.trak
    Weblogs that reference this entry
    • None