• <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ù)  更多評論
              

            国产91色综合久久免费| 亚洲欧美日韩久久精品第一区| 久久99精品久久久久久久不卡| 波多野结衣中文字幕久久| 国产 亚洲 欧美 另类 久久| 日产久久强奸免费的看| 人人狠狠综合久久88成人| 国产精品九九久久免费视频 | 国产精品九九久久精品女同亚洲欧美日韩综合区 | 久久九九精品99国产精品| 精品国产一区二区三区久久蜜臀| 亚洲国产成人久久综合碰| 国产精品一久久香蕉产线看| 深夜久久AAAAA级毛片免费看| 国产精品九九九久久九九| 国产精品久久久久久五月尺| 久久久久久无码Av成人影院| 四虎影视久久久免费| 91久久精品国产91性色也| 欧美亚洲色综久久精品国产| 亚洲欧洲久久av| 久久九九久精品国产免费直播| 99久久精品费精品国产一区二区 | 久久精品麻豆日日躁夜夜躁| 久久婷婷人人澡人人| 国产精品美女久久久久AV福利| 久久综合国产乱子伦精品免费| 四虎影视久久久免费| 日韩欧美亚洲综合久久影院Ds | 国产AⅤ精品一区二区三区久久| 久久精品国产亚洲AV高清热| 狠狠综合久久综合88亚洲 | 青青草原综合久久大伊人| 亚洲&#228;v永久无码精品天堂久久| 久久成人精品视频| 久久久久国产一级毛片高清版| 国产精品久久久亚洲| 精品久久久久中文字幕日本| 精品午夜久久福利大片| 91久久精品视频| 性做久久久久久久久|