• <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++博客 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
              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 閱讀(797) 評(píng)論(1)  編輯 收藏 引用 所屬分類: 1.2 閑人雜語(yǔ)

            Feedback

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

            狠狠干狠狠久久| 久久国产欧美日韩精品| 精品久久一区二区三区| 精品999久久久久久中文字幕| 国产精品久久久久久影院| 久久本道久久综合伊人| 久久精品无码一区二区app| 合区精品久久久中文字幕一区| 亚洲精品无码久久千人斩| 久久久精品免费国产四虎| 久久精品成人欧美大片| 久久免费看黄a级毛片| 日本三级久久网| 日产精品久久久久久久性色| 九九99精品久久久久久| 中文字幕无码精品亚洲资源网久久| a高清免费毛片久久| 性欧美丰满熟妇XXXX性久久久| 久久综合综合久久狠狠狠97色88| 久久亚洲2019中文字幕| 97久久久久人妻精品专区 | 漂亮人妻被黑人久久精品| 97久久超碰国产精品旧版| 中文成人久久久久影院免费观看| 国产一区二区精品久久| 99蜜桃臀久久久欧美精品网站 | 久久久黄片| 九九久久自然熟的香蕉图片| 亚洲精品国精品久久99热| 国产精品久久久久AV福利动漫| 亚洲国产精品成人久久蜜臀 | 久久99精品久久久久久久久久| 无码国内精品久久人妻麻豆按摩| 麻豆精品久久精品色综合| 国产91色综合久久免费| av午夜福利一片免费看久久 | 亚洲精品无码久久久久去q| 一日本道伊人久久综合影| 色偷偷88欧美精品久久久| 久久综合偷偷噜噜噜色| 久久久久免费精品国产|