• <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 黑色天使 閱讀(487) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式

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

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久综合狠狠综合久久| 久久Av无码精品人妻系列| 成人免费网站久久久| 国产精品免费久久久久影院| 国产精品99久久久久久宅男| 亚洲精品无码久久毛片| 97久久国产综合精品女不卡| 久久99国产精品久久99| 一本久久免费视频| 免费观看成人久久网免费观看| 无码人妻久久一区二区三区蜜桃| 久久久久波多野结衣高潮| 国产成人精品久久亚洲| 99久久99久久精品国产片果冻| 777久久精品一区二区三区无码| 人人狠狠综合88综合久久| 欧美va久久久噜噜噜久久| 日本精品久久久久久久久免费| AV无码久久久久不卡网站下载| 伊人久久大香线蕉综合热线| 国产精品久久久久…| 精品一二三区久久aaa片| 久久久综合香蕉尹人综合网| 国产精品一久久香蕉国产线看观看 | 久久久久女教师免费一区| 亚洲午夜久久久影院伊人| 久久久久亚洲AV综合波多野结衣 | 久久99精品久久久久久| 久久人人爽人人精品视频| 国产三级精品久久| 亚洲狠狠久久综合一区77777| 国产精品欧美久久久天天影视| 久久精品国产亚洲AV影院| 国产精品久久久久免费a∨| 久久中文字幕视频、最近更新| 久久精品国产色蜜蜜麻豆| 国产 亚洲 欧美 另类 久久| 免费国产99久久久香蕉| 亚洲国产精品久久久久婷婷老年| 久久香蕉综合色一综合色88| 久久亚洲国产欧洲精品一|