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

            那誰的技術博客

            感興趣領域:高性能服務器編程,存儲,算法,Linux內核
            隨筆 - 210, 文章 - 0, 評論 - 1183, 引用 - 0
            數據加載中……

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

            作用:
            運用共享技術有效地支持大量細粒度的對象。

            UML結構圖:



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

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

            1)Flyweight.h
            /********************************************************************
            ????created:????2006/07/26
            ????filename:?????FlyWeight.h
            ????author:????????李創
            ????????????????
            http://www.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:????????李創
            ????????????????
            http://www.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:????????李創
            ????????????????
            http://www.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 那誰 閱讀(2344) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式

            国产精品免费久久久久影院 | 伊人久久综在合线亚洲2019| 精品国产乱码久久久久软件| 久久93精品国产91久久综合| 久久久精品一区二区三区| 久久成人国产精品| 久久人人爽人人人人片av| 亚洲国产香蕉人人爽成AV片久久| 久久不见久久见免费影院www日本| 99热精品久久只有精品| 久久99精品综合国产首页| 色综合久久最新中文字幕| 国产国产成人精品久久| 99久久中文字幕| 久久综合综合久久97色| 91精品国产色综久久| 久久人人爽人人爽人人片AV麻豆| 精品久久久久国产免费| 午夜福利91久久福利| 成人综合久久精品色婷婷| 国产亚洲美女精品久久久2020| 色8久久人人97超碰香蕉987| 狠狠久久亚洲欧美专区| 精品人妻伦九区久久AAA片69| 久久噜噜久久久精品66| 中文字幕无码久久精品青草| 囯产极品美女高潮无套久久久 | 久久国产精品无码一区二区三区| 国产三级久久久精品麻豆三级| 国产精品久久久亚洲| 精品久久久久中文字| 亚洲愉拍99热成人精品热久久| 久久国产免费观看精品3| 国产女人aaa级久久久级| 热RE99久久精品国产66热| 久久夜色精品国产噜噜噜亚洲AV| 精品国产VA久久久久久久冰| 久久婷婷色综合一区二区| 午夜精品久久久久久久久| 激情五月综合综合久久69| 国产色综合久久无码有码|