• <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++博客 首頁 新隨筆 聯(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 閱讀(806) 評論(1)  編輯 收藏 引用 所屬分類: 1.2 閑人雜語

            Feedback

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

            成人国内精品久久久久影院VR| 久久精品国产亚洲7777| 久久精品国产亚洲77777| www.久久热.com| 一级女性全黄久久生活片免费| 亚洲AV无码久久精品狠狠爱浪潮| 热久久这里只有精品| 免费无码国产欧美久久18| 久久99精品综合国产首页| 亚洲国产天堂久久综合| 久久国产精品-国产精品| 久久精品国产亚洲AV忘忧草18 | 热久久国产欧美一区二区精品| 久久精品国产99国产精品导航 | 亚洲午夜久久久久久噜噜噜| 国产精品gz久久久| 99久久久国产精品免费无卡顿| 欧美精品一区二区久久| 中文字幕久久欲求不满| 久久精品国产亚洲av麻豆小说| 久久无码AV一区二区三区| 久久本道久久综合伊人| 精品熟女少妇av免费久久| 一本一本久久A久久综合精品| 亚洲欧美另类日本久久国产真实乱对白| 91久久精品91久久性色| 国内精品久久人妻互换| 亚洲中文字幕久久精品无码APP| 国产精品久久久久久久久久影院| 久久国产精品偷99| 欧美粉嫩小泬久久久久久久 | 伊人色综合久久天天网| 日韩欧美亚洲国产精品字幕久久久 | 久久国产精品免费一区| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 国产91色综合久久免费| 亚洲va久久久噜噜噜久久男同 | 人人狠狠综合久久亚洲| 久久久国产亚洲精品| 久久99热这里只有精品国产| 日韩精品久久久肉伦网站|