• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            子彈 の VISIONS

            NEVER back down ~~

            C++博客 首頁 新隨筆 聯系 聚合 管理
              112 Posts :: 34 Stories :: 99 Comments :: 0 Trackbacks

            Factory Method

            // interface
            class People
            {
            public:
                virtual void doWhat() =0;
                virtual string type() const =0;
            };

            class Male : public People
            {
            public:
                virtual void doWhat();
                virtual string type() const;
                static  People* creator();
            };

            class Female : public People
            {
            public:
                virtual void doWhat();
                virtual string type() const;
                static  People* creator();
            };


            // interface
            typedef People* (*CreateFcn)();
            class PeopleFactory
            {
            public:
                static People* producePeople(const string& type);
                static addCreateFcn(const string& type, CreateFcn creator);

            private:
                static map<string, CreateFcn> createFcns;
            };

            People* PeopleFactory::producePeople(const string& type)
            {
                //***1
                CreateFcn creator= createFcns[type];
                return creator();
            }

            ---- test ----
            void f()
            {
                //***2
                const string t("Male"); //or "Female"
                People* p = PeopleFactory::producePeople(t);
                p->doWhat();   
                delete p;
            }

            ---- extension ----
            class Hemophrodite : public People
            {
                virtual void doWhat();
                virtual string type() const;
                static  People* creator();
            };

            void g()
            {
                //***3
                const string newType = "Hemophrodite";
                PeopleFactory::addCreateFcn(newType, Hemophrodite::creator);

                // usage
                const string t("Hemophrodite");
                People* p = PeopleFactory::producePeople(t);
                p->doWhat();   
                delete p;
            }

            Cool!! OCP!!

            ---------
            Key point:

            1. How does the Factory Method create the instance
                of classes through their type names?
               How could this method follow the OCP?

            2. Where does the type name come from?
             -- config file
             -- registry
             -- other method

            3. About extension
              
            ----------
            Interface classes:
                People & PeopleFactory


            enjoy it!!
            posted on 2008-07-17 12:43 子彈のVISIONS 閱讀(814) 評論(1)  編輯 收藏 引用 所屬分類: 1.2 閑人雜語

            Feedback

            # re: [設計模式] Understand Design Patterns -- Factory Method 2014-07-06 17:07 Emma
            用什么可以提供給那些誰需要,我相信你的網站是好的。 http://www.friv5gamer.com  回復  更多評論
              

            久久久精品日本一区二区三区 | 日韩影院久久| 国内精品久久久久国产盗摄| 婷婷久久综合九色综合98| 久久精品嫩草影院| 国产伊人久久| 亚洲国产精品无码久久久秋霞2 | 亚洲欧美久久久久9999| 久久精品国产免费观看三人同眠| 亚洲国产精品嫩草影院久久| 久久久久久久人妻无码中文字幕爆 | 久久天天躁狠狠躁夜夜躁2O2O| 久久亚洲AV成人无码电影| 亚洲成色www久久网站夜月 | 久久91精品综合国产首页| 亚洲欧美日韩精品久久| 午夜精品久久久久久99热| 国内精品久久久久久久影视麻豆| 久久香蕉国产线看观看精品yw| 久久精品这里只有精99品| 久久久久国产一级毛片高清版| yy6080久久| 亚洲AV无码久久| 久久亚洲精品成人AV| 久久精品一本到99热免费| 亚洲人成网亚洲欧洲无码久久| 欧美日韩精品久久久久| 精品久久亚洲中文无码| 婷婷久久久亚洲欧洲日产国码AV| 久久久久久久久久久精品尤物 | 久久青青草原精品国产| 新狼窝色AV性久久久久久| 国内精品久久久久影院优| 久久精品国产精品亚洲艾草网美妙 | 亚洲AV无一区二区三区久久 | 国色天香久久久久久久小说| 国产成人综合久久精品红| 久久亚洲AV成人无码电影| 久久久久99精品成人片牛牛影视| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 狠狠88综合久久久久综合网|