• <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>
            蝸牛的家
            男兒當自強
            posts - 48,  comments - 21,  trackbacks - 0
            意圖:
            運用共享技術有效地支持大量細粒度的對象
            適用:
            一個應用程序使用了大量的對象
            完全由于使用大量的對象,造成很大的存儲開銷
            對象的大多數狀態都可變為外部狀態
            如果刪除對象的外部狀態,那么可以用相對較少的共享對象取代很多組對象
            UML

            解析:
            Flywweight模式大量使用在當一些可以被共享的對象經常使用的情況下
            //test.h

            #include 
            <string>
            #include 
            <list>
            //////////////////////////////////////////////////////////////////////////
            using namespace std;

            class Flyweight
            {
            public:
                
            virtual ~Flyweight(){}
                
                
            string GetIntrinsicState();
                
            virtual void Operation(string& ExtrinsicState) = 0;
            protected:
                Flyweight(
            const string& state) : m_state(state){}
            private:
                
            string m_state;
            }
            ;

            class FlyweightFactory
            {
            public:
                FlyweightFactory()
            {}
                
            ~FlyweightFactory();
                
                Flyweight
            * GetFlyweight(const string& key);
            private:
                list
            <Flyweight*> m_listFlyweight;
            }
            ;

            class ConCreateFlyweight : public Flyweight
            {
            public:
                ConCreateFlyweight(
            const string& state) : Flyweight(state){}
                
            virtual ~ConCreateFlyweight(){}
                
                
            virtual void Operation(string& ExtrinsicState);
            }
            ;
            // test.cpp : Defines the entry point for the console application.
            //

            #include 
            "stdafx.h"
            #include 
            <iostream>
            #include 
            "test.h"

            using namespace std;
            //////////////////////////////////////////////////////////////////////////
            inline string Flyweight::GetIntrinsicState()
            {
                
            return m_state;
            }


            FlyweightFactory::
            ~FlyweightFactory()
            {
                list
            <Flyweight*>::iterator iter1,iter2,temp;
                
            for (iter1 = m_listFlyweight.begin(),iter2 = m_listFlyweight.end(); iter1 != iter2;)
                
            {
                    temp 
            = iter1;
                    
            ++iter1;
                    delete(
            *temp);
                }

                m_listFlyweight.clear();
            }


            Flyweight
            * FlyweightFactory::GetFlyweight(const string& key)
            {
                list
            <Flyweight*>::iterator iter1,iter2;
                
            //查看列表中是否有存在的對象
                for (iter1 = m_listFlyweight.begin(),iter2 = m_listFlyweight.end(); iter1 != iter2; ++iter1)
                
            {
                    
            if ((*iter1)->GetIntrinsicState() == key)
                    
            {
                        cout 
            <<"The Flyweight:"<<key<<"already exits"<<endl;
                        
            return (*iter1);
                    }

                }

                cout 
            << "Creating a new Flyweight:"<<key<<endl;
                Flyweight
            * pFlyweight = new ConCreateFlyweight(key);
                m_listFlyweight.push_back(pFlyweight);
            }


            void ConCreateFlyweight::Operation(string& ExtrinsicState)
            {
            }

            //////////////////////////////////////////////////////////////////////////
            int main(int argc, char* argv[])
            {
                
                FlyweightFactory flyweightFactory;
                flyweightFactory.GetFlyweight(
            "hello");
                flyweightFactory.GetFlyweight(
            "world");
                flyweightFactory.GetFlyweight(
            "hello");
                
                system(
            "pause");
                
            return 0;
            }


            posted on 2008-08-20 22:54 黑色天使 閱讀(486) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式

            <2008年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            99久久精品国产免看国产一区| 亚洲国产成人乱码精品女人久久久不卡| 久久精品国产亚洲精品| 精品国产综合区久久久久久 | 色综合久久无码五十路人妻| 久久66热人妻偷产精品9| 日本精品久久久中文字幕| 久久久精品国产亚洲成人满18免费网站 | 97精品伊人久久久大香线蕉| 久久精品国产只有精品2020| 波多野结衣久久| 18岁日韩内射颜射午夜久久成人| 大香伊人久久精品一区二区| 亚洲国产精品一区二区久久| 伊人久久大香线蕉AV色婷婷色| 精品久久久久久无码中文野结衣| 久久人与动人物a级毛片| 一本色道久久88加勒比—综合| 成人综合久久精品色婷婷| 91麻精品国产91久久久久| 性高湖久久久久久久久| 欧美午夜精品久久久久久浪潮| 99精品国产在热久久| 亚洲国产精品久久久天堂| 久久无码高潮喷水| 色99久久久久高潮综合影院| 26uuu久久五月天| 91久久香蕉国产熟女线看| 97久久超碰国产精品2021| 久久无码人妻一区二区三区 | 热re99久久精品国产99热| 国产精品9999久久久久| AV无码久久久久不卡蜜桃| 久久久午夜精品| 香蕉久久夜色精品国产2020| 久久久午夜精品福利内容| 亚洲国产高清精品线久久| 中文成人久久久久影院免费观看| 亚洲国产高清精品线久久| 欧美国产成人久久精品| 亚洲人成伊人成综合网久久久 |