• <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
            意圖:
            運用共享技術有效地支持大量細粒度的對象
            適用:
            一個應用程序使用了大量的對象
            完全由于使用大量的對象,造成很大的存儲開銷
            對象的大多數(shù)狀態(tài)都可變?yōu)橥獠繝顟B(tài)
            如果刪除對象的外部狀態(tài),那么可以用相對較少的共享對象取代很多組對象
            UML

            解析:
            Flywweight模式大量使用在當一些可以被共享的對象經(jīng)常使用的情況下
            //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 黑色天使 閱讀(483) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式

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

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲国产成人久久精品99 | www.久久热.com| 99久久精品国产综合一区 | 亚洲欧洲久久久精品| 72种姿势欧美久久久久大黄蕉| 欧美日韩精品久久久久| 精品久久一区二区| 欧美噜噜久久久XXX| 亚洲七七久久精品中文国产| 国产AV影片久久久久久| 99国产欧美精品久久久蜜芽| 99久久无色码中文字幕人妻| 久久久久国产一区二区三区| 91久久成人免费| 一本一本久久A久久综合精品 | 国产精品久久久99| 东京热TOKYO综合久久精品| 精品久久久久久中文字幕大豆网| 久久久久久久国产免费看| 久久精品国产一区| 国产精品岛国久久久久| 国产精品女同久久久久电影院| 无遮挡粉嫩小泬久久久久久久| 久久99久国产麻精品66| 久久无码高潮喷水| 欧美亚洲国产精品久久高清| 伊人色综合久久天天人守人婷| 无码任你躁久久久久久久| 色天使久久综合网天天| 伊人久久大香线蕉无码麻豆| 国产精品99久久久久久宅男小说| 亚洲精品无码久久久久AV麻豆| 色婷婷综合久久久久中文字幕| 亚洲国产精品一区二区三区久久| 久久亚洲AV永久无码精品| 人人妻久久人人澡人人爽人人精品 | 精品久久久久久久久午夜福利| 国产亚洲欧美成人久久片| 久久99久久99小草精品免视看| 精品国产婷婷久久久| 99精品国产免费久久久久久下载|