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

            那誰的技術(shù)博客

            感興趣領(lǐng)域:高性能服務(wù)器編程,存儲,算法,Linux內(nèi)核
            隨筆 - 210, 文章 - 0, 評論 - 1183, 引用 - 0
            數(shù)據(jù)加載中……

            常見設(shè)計模式的解析和實現(xiàn)(C++)之十三-FlyWeight模式

            作用:
            運用共享技術(shù)有效地支持大量細(xì)粒度的對象。

            UML結(jié)構(gòu)圖:



            解析:
            Flyweight模式在大量使用一些可以被共享的對象的時候經(jīng)常使用.比如,在QQ聊天的時候很多時候你懶得回復(fù)又不得不回復(fù)的時候,一般會用一些客套的話語敷衍別人,如"呵呵","好的"等等之類的,這些簡單的答復(fù)其實每個人都是提前定義好的,在使用的時候才調(diào)用出來.Flyweight就是基于解決這種問題的思路而產(chǎn)生的,當(dāng)需要一個可以在其它地方共享使用的對象的時候,先去查詢是否已經(jīng)存在了同樣的對象,如果沒有就生成之有的話就直接使用.因此,Flyweight模式和Factory模式也經(jīng)常混用.

            實現(xiàn):
            需要說明的是下面的實現(xiàn)僅僅實現(xiàn)了對可共享對象的使用,非可共享對象的使用沒有列出,因為這個不是Flyweight模式的重點.這里的實現(xiàn)要點是采用一個list鏈表來保存這些可以被共享的對象,需要使用的時候就到鏈表中查詢是不是已經(jīng)存在了,如果不存在就初始化一個,然后返回這個對象的指針.

            1)Flyweight.h
            /********************************************************************
            ????created:????2006/07/26
            ????filename:?????FlyWeight.h
            ????author:????????李創(chuàng)
            ????????????????
            http://m.shnenglu.com/converse/

            ????purpose:????FlyWeight模式的演示代碼
            ********************************************************************
            */


            #ifndef?FLYWEIGHT_H
            #define?FLYWEIGHT_H

            #include?
            <string>
            #include?
            <list>

            typedef?std::
            string?STATE;

            class?Flyweight
            {
            public:
            ????
            virtual?~Flyweight(){}

            ????STATE?GetIntrinsicState();
            ????
            virtual?void?Operation(STATE&?ExtrinsicState)?=?0;

            protected:
            ????Flyweight(
            const?STATE&?state)?
            ????????:m_State(state)
            ????
            {
            ????}


            private:
            ????STATE?m_State;
            }
            ;

            class?FlyweightFactory
            {
            public:
            ????FlyweightFactory()
            {}
            ????
            ~FlyweightFactory();

            ????Flyweight
            *?GetFlyweight(const?STATE&?key);

            private:
            ????std::list
            <Flyweight*>????m_listFlyweight;
            }
            ;

            class?ConcreateFlyweight
            ????:?
            public?Flyweight
            {
            public:
            ????ConcreateFlyweight(
            const?STATE&?state)
            ????????:?Flyweight(state)
            ????
            {
            ????}

            ????
            virtual?~ConcreateFlyweight(){}

            ????
            virtual?void?Operation(STATE&?ExtrinsicState);
            }
            ;

            #endif

            2)Flyweight.cpp
            /********************************************************************
            ????created:????2006/07/26
            ????filename:?????FlyWeight.cpp
            ????author:????????李創(chuàng)
            ????????????????
            http://m.shnenglu.com/converse/

            ????purpose:????FlyWeight模式的演示代碼
            ********************************************************************
            */


            #include?
            "FlyWeight.h"
            #include?
            <iostream>

            inline?STATE?Flyweight::GetIntrinsicState()
            {
            ????
            return?m_State;
            }


            FlyweightFactory::
            ~FlyweightFactory()
            {
            ????std::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?STATE&?key)
            {
            ????std::list
            <Flyweight*>::iterator?iter1,?iter2;

            ????
            for?(iter1?=?m_listFlyweight.begin(),?iter2?=?m_listFlyweight.end();
            ?????????iter1?
            !=?iter2;
            ?????????
            ++iter1)
            ????
            {
            ????????
            if?((*iter1)->GetIntrinsicState()?==?key)
            ????????
            {
            ????????????std::cout?
            <<?"The?Flyweight:"?<<?key?<<?"?already?exits"<<?std::endl;
            ????????????
            return?(*iter1);
            ????????}

            ????}


            ????std::cout?
            <<?"Creating?a?new?Flyweight:"?<<?key?<<?std::endl;
            ????Flyweight
            *?flyweight?=?new?ConcreateFlyweight(key);
            ????m_listFlyweight.push_back(flyweight);
            }


            void?ConcreateFlyweight::Operation(STATE&?ExtrinsicState)
            {

            }


            3)Main.cpp
            /********************************************************************
            ????created:????2006/07/26
            ????filename:?????Main.cpp
            ????author:????????李創(chuàng)
            ????????????????
            http://m.shnenglu.com/converse/

            ????purpose:????FlyWeight模式的測試代碼
            ********************************************************************
            */


            #include?
            "FlyWeight.h"

            int?main()
            {
            ????FlyweightFactory?flyweightfactory;
            ????flyweightfactory.GetFlyweight(
            "hello");
            ????flyweightfactory.GetFlyweight(
            "world");
            ????flyweightfactory.GetFlyweight(
            "hello");

            ????system(
            "pause");
            ????
            return?0;
            }

            posted on 2006-08-03 21:12 那誰 閱讀(2352) 評論(0)  編輯 收藏 引用 所屬分類: 設(shè)計模式

            久久亚洲AV无码精品色午夜| 久久精品亚洲一区二区三区浴池 | 97久久超碰国产精品2021| 亚洲国产精品无码久久久不卡| 7777久久久国产精品消防器材| 久久久久亚洲av无码专区导航| 青青草原综合久久大伊人精品| 九九热久久免费视频| 国产成人精品久久| 国内精品久久国产大陆| 一本色道久久88综合日韩精品 | 久久久久久国产精品美女| 狠狠色丁香久久综合婷婷| 久久影院亚洲一区| 狠狠色丁香久久综合婷婷| 一本一本久久a久久精品综合麻豆| 久久人人爽人人爽人人片AV高清 | 久久婷婷午色综合夜啪| 国产精品一久久香蕉国产线看观看| 精品久久久久久久中文字幕 | 性高湖久久久久久久久| 日本亚洲色大成网站WWW久久 | 精品多毛少妇人妻AV免费久久| 精品熟女少妇AV免费久久| 久久亚洲国产精品123区| 欧美综合天天夜夜久久| 久久久久久国产精品美女| 亚洲国产成人乱码精品女人久久久不卡| 国产国产成人精品久久| 欧美va久久久噜噜噜久久| 亚洲午夜久久久| 久久婷婷是五月综合色狠狠| 伊人伊成久久人综合网777| 日批日出水久久亚洲精品tv| 99久久国产亚洲高清观看2024| 精品久久久久香蕉网| 久久精品人人做人人爽97| 久久久久人妻一区精品性色av| 亚洲伊人久久大香线蕉综合图片 | 77777亚洲午夜久久多喷| 久久国产欧美日韩精品|