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

            隨筆分類

            隨筆檔案

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            欧美粉嫩小泬久久久久久久 | 丁香久久婷婷国产午夜视频| 精品久久久久久综合日本| 97久久精品人人做人人爽| 免费无码国产欧美久久18| 99久久精品费精品国产一区二区| 国产精品久久久久一区二区三区| 无码八A片人妻少妇久久| 三上悠亚久久精品| 看全色黄大色大片免费久久久| 国产一区二区三区久久| 日批日出水久久亚洲精品tv| 国内精品久久久久影院一蜜桃| 亚洲中文字幕无码一久久区| 中文字幕成人精品久久不卡| 久久久无码精品亚洲日韩京东传媒| 久久久久久综合一区中文字幕| 亚洲中文字幕无码久久2020| 日本欧美国产精品第一页久久| 香蕉久久一区二区不卡无毒影院| 亚洲va久久久噜噜噜久久男同| 久久久精品日本一区二区三区| 婷婷综合久久狠狠色99h| 人妻无码αv中文字幕久久| 人妻无码αv中文字幕久久琪琪布| 九九热久久免费视频| av午夜福利一片免费看久久| 久久亚洲欧美国产精品| 狠狠色噜噜色狠狠狠综合久久| 亚洲中文字幕无码久久综合网 | av无码久久久久不卡免费网站 | 俺来也俺去啦久久综合网| 色88久久久久高潮综合影院| 亚洲精品无码久久千人斩| 亚洲va久久久噜噜噜久久男同| 伊人久久大香线蕉综合影院首页| 一本色道久久HEZYO无码| 欧洲成人午夜精品无码区久久| 国产精品美女久久久久久2018| 97久久精品国产精品青草| 久久久久99精品成人片试看|