• <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>

            清風竹林

            ぷ雪飄絳梅映殘紅
               ぷ花舞霜飛映蒼松
                 ----- Do more,suffer less

            通用工廠類(generic factory class)

              舊項目結(jié)束,新項目開始,今天開始閱讀新項目相關(guān)的代碼。公司的代碼中大量的運用了工廠技術(shù),不過很可惜是simple factory,這樣當你新加入一種對象類型的時候都需要去工廠的實現(xiàn)代碼(實際上在舊代碼實現(xiàn)中還不止如此,乃至于公用父類也需要修改)。這是一件不太好的事情,于是我希望在自己的代碼中重新實現(xiàn),以減少維護量。
              我記得《C++對話系列》中有一些相關(guān)的討論,于是我找來查看。非常幸運的,我發(fā)現(xiàn)這正是我需要的東西,于是幾乎照抄的把代碼實現(xiàn)了,代碼測試通過。為了符合我自己的閱讀習慣,我把其中的一個函數(shù)及變量的命名改動了一下:
              factory.hpp
            #pragma once
            #pragma warning (disable:
            4786)
            /********************************************************************
            created:    2008-10-22
            author:        lixianmin

            purpose:    a generic factory method lib
            reference:  《C++對話系列--Abstract Factory, Template Style》
            Copyright (C) 2008 - All Rights Reserved
            ********************************************************************
            */
            #include 
            <string>
            #include 
            <map>
            namespace lib
            {
                
            //通用工廠類,根據(jù)傳入的id創(chuàng)建產(chǎn)品對象
                template <typename BaseType, typename KeyType=std::string>
                
            class factory
                {
                
            private:
                    typedef std::auto_ptr
            <BaseType> (*BaseCreateFunc)();
                    typedef std::map
            <KeyType, BaseCreateFunc> FuncRegistry;
                
            public:
                    
            static factory<BaseType, KeyType>& get_instance()        //static singleton,不適用于多線程
                    {
                        
            static factory<BaseType, KeyType> obj;
                        
            return obj;
                    }
                    std::auto_ptr
            <BaseType> create(const KeyType& id) const
                    {
                        std::auto_ptr
            <BaseType> obj;
                        FuncRegistry::const_iterator regEntry 
            = _registry.find(id);
                        
            if (regEntry != _registry.end()) 
                        {
                            obj 
            = regEntry->second();
                        }
                        
            return obj;
                    }
                    
            void _register_create_function(const KeyType& id, BaseCreateFunc func){_registry[id] = func;}
                
            private:
                    factory(
            void){}
                    factory(
            const factory& other);
                    factory 
            operator=(const factory& other);
                
            private:
                    FuncRegistry _registry;
                };
                
            //類型(DerivedType)注冊類,只要在DerivedType類定義(DerivedType.cpp文件)的最后聲明一個 lib::factory_register<Base, Derived> reg(id);對象即可
                template <typename BaseType, typename DerivedType, typename KeyType=std::string>
                
            class factory_register
                {
                
            public:
                    factory_register(
            const KeyType& id)
                    {
                        factory
            <BaseType, KeyType>::get_instance()._register_create_function(id, _create_instance);
                    }
                
            private:
                    
            static std::auto_ptr<BaseType> _create_instance(){return std::auto_ptr<BaseType>(new DerivedType);}
                
            private:
                    friend 
            class factory<BaseType, KeyType>;
                };
            }

            測試代碼:
            #include <iostream>
            #include 
            "factory.hpp"
            //基類
            class Base
            {
            public:
                
            virtual void print(void)
                {
                    std::cout
            <<"base class"<<std::endl;
                }
            };
            lib::factory_register
            <Base, Base> reg1("base");            //注冊Base類型,定義一個file scope的變量,通常應該置于Base.cpp文件結(jié)尾
            //派生類
            class Derived:public Base
            {
            public:
                
            virtual void print(void)
                {
                    std::cout
            <<"derived class"<<std::endl;
                }
            };
            lib::factory_register
            <Base, Derived> reg2("derived");    //注冊Derived類型,定義一個file scope的變量,通常應該置于Derived.cpp文件結(jié)尾

            //聲明一個全局函數(shù)(簡化操作,非必需)
            lib::factory<Base>& glb_GetFactory(void)
            {
                
            return lib::factory<Base>::get_instance();
            }
            int main(int argc, char* argv[])
            {
                std::auto_ptr
            <Base> base=glb_GetFactory().create("base");
                
            if (base.get())
                {
                    
            base->print();
                }
                std::auto_ptr
            <Base> derived=glb_GetFactory().create("derived");
                
            if (derived.get())
                {
                    derived
            ->print();
                }

                system(
            "pause");
                
            return 0;
            }
            輸出:
            base class
            derived class
            請按任意鍵繼續(xù). . .




            posted on 2008-10-22 18:48 李現(xiàn)民 閱讀(1998) 評論(2)  編輯 收藏 引用 所屬分類: 語法試煉

            評論

            # re: 通用工廠類(generic factory class) 2008-10-23 09:29 RichardHe

            嗯..不錯不錯..沒有3年C++經(jīng)驗是很難寫出這樣的東東的啊..
            向你學習了!  回復  更多評論   

            # re: 通用工廠類(generic factory class) 2008-10-23 10:51 李現(xiàn)民

            @RichardHe
            這個,前面敘述中已經(jīng)說過了,“基本上是原版的別人的代碼”,汗一個  回復  更多評論   

            色婷婷久久综合中文久久一本| 久久久久亚洲AV无码专区体验| 91久久精品视频| 久久这里只有精品视频99| 久久精品青青草原伊人| 精品久久久久久国产潘金莲| 日韩一区二区久久久久久 | 亚洲国产精品人久久| 人妻中文久久久久| 99久久超碰中文字幕伊人| 久久国产精品偷99| 久久99久久99精品免视看动漫| 成人精品一区二区久久| 久久综合国产乱子伦精品免费| 久久久久国色AV免费观看 | 久久精品综合一区二区三区| 亚洲国产精品高清久久久| 久久久99精品成人片中文字幕| 精品久久久久久无码专区| 亚洲国产成人精品女人久久久 | 精品人妻伦九区久久AAA片69| 中文字幕热久久久久久久| 久久久久久国产精品无码下载| 久久精品国产亚洲AV高清热| 久久精品国产免费观看| 香蕉aa三级久久毛片| 欧美精品丝袜久久久中文字幕| 色综合久久综精品| 久久综合丁香激情久久| 99久久人妻无码精品系列蜜桃| 亚洲精品无码久久久久| 综合网日日天干夜夜久久| 亚洲欧美国产精品专区久久| 久久亚洲天堂| 亚洲精品无码久久毛片| 热综合一本伊人久久精品| 久久婷婷人人澡人人| 亚洲七七久久精品中文国产| 国产精品久久久久久久人人看| 久久婷婷是五月综合色狠狠| 国内精品伊人久久久久妇|