• <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  回復  更多評論
              

            国产成人精品久久免费动漫| 国产L精品国产亚洲区久久| 国内精品久久久久久久久电影网| 麻豆久久久9性大片| 日产精品99久久久久久| 国产精品免费久久| 久久99精品国产自在现线小黄鸭 | 久久一本综合| 久久久久久亚洲AV无码专区| 久久这里有精品视频| 久久九九亚洲精品| 久久久免费精品re6| 波多野结衣久久| 亚洲美日韩Av中文字幕无码久久久妻妇 | 久久国产免费观看精品3| 久久综合九色综合久99| 久久国产精品久久久| 色欲综合久久躁天天躁蜜桃| 久久久久国色AV免费看图片| 久久国产精品99久久久久久老狼 | 99久久免费国产精精品| 日韩av无码久久精品免费| 久久精品卫校国产小美女| 欧美日韩精品久久久久| 欧美精品九九99久久在观看| 久久亚洲2019中文字幕| 亚洲精品99久久久久中文字幕 | 久久久艹| 久久无码一区二区三区少妇| 欧美日韩成人精品久久久免费看| 91精品国产91久久久久久| 欧美精品一本久久男人的天堂| 久久国产乱子伦免费精品| 久久久久亚洲av无码专区| 久久婷婷五月综合97色| 久久久久久国产精品免费无码| 乱亲女H秽乱长久久久| av无码久久久久不卡免费网站| 97久久精品人妻人人搡人人玩| 久久精品欧美日韩精品| 国产亚洲欧美成人久久片|