• <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ù)博客

            聯(lián)系 聚合 管理
              154 Posts :: 1 Stories :: 561 Comments :: 0 Trackbacks

                                                                                      DownLoad
            OGRE
            分析之設(shè)計(jì)模式(四)

            Mythma

             Email: mythma@163.com

                  OGRE的設(shè)計(jì)結(jié)構(gòu)十分清晰,這得歸功于設(shè)計(jì)模式的成功運(yùn)用。

            八、Iterator

            說到Iterator,讓人首先想到的是STL中各種iteratorsOGRE源碼中廣泛用到了STL,尤其是容器map。但OGRE大部分情況下并沒有直接使用與容器配套的迭代器,而是在iterator上包了一層。對(duì)序列式容器的iteratorOGRE包裝為VectorIterator<T>,其const形式為ConstVectorIterator;對(duì)關(guān)聯(lián)式容器(map),包裝為MapIterator<T>,其const形式為ConstMapIterator。所以從另一個(gè)角度看,使用的是Adapter模式。

            OGRE的包裝本身沒有什么復(fù)雜,看一下mapiterator封裝就清楚了:

               template <class T>
                
            class MapIterator
                
            {
                
            private:
                    
            typename T::iterator mCurrent;
                  
              typename T::iterator mEnd;
                    /**//// Private constructor since only the parameterised constructor should be used
                    MapIterator()  {};
                
            public:
                 
               typedef typename T::mapped_type MappedType;
                    typedef typename T::key_type KeyType;
                    /**//** Constructor.
                    @remarks
                        Provide a start and end iterator to initialise.
                    */

                    MapIterator(typename T::iterator start, typename T::iterator end)
                        : mCurrent(start), mEnd(end)
                    
            {
                    }
                    
            /**//** Returns true if there are more items in the collection. */
                    
            bool hasMoreElements(voidconst
                    
            {
                        
            return mCurrent != mEnd;
                    }
                    
            /**//** Returns the next value element in the collection, and advances to the next. */
                    typename T::mapped_type getNext(
            void)
                    
            {
                        
            return (mCurrent++)->second;
                    }
                    
            /**//** Returns the next value element in the collection, without advancing to the next. */
                    typename T::mapped_type peekNextValue(
            void)
                    
            {
                        
            return mCurrent->second;
                    }
                    
            /**//** Returns the next key element in the collection, without advancing to the next. */
                    typename T::key_type peekNextKey(
            void)
                    
            {
                        
            return mCurrent->first;
                    }
                    
            /**//** Required to overcome intermittent bug */
                     MapIterator<T> & 
            operator=( MapIterator<T> &rhs )
                     
            {
                         mCurrent = rhs.mCurrent;
                         mEnd = rhs.mEnd;
                         
            return *this;
                     }
                    
            /**//** Returns a pointer to the next value element in the collection, without 
                        advancing to the next afterwards. */

                    typename T::pointer peekNextValuePtr(
            void)
                    
            {
                        
            return &(mCurrent->second);
                    }
                    
            /**//** Moves the iterator on one element. */
                    
            void moveNext(void)
                    
            {
                        mCurrent++;
                    }

             };

             

            九、Observer

                  Observer模式“定義對(duì)象間一對(duì)多的依賴關(guān)系,當(dāng)一個(gè)對(duì)象的狀態(tài)發(fā)生變化時(shí),所有依賴他的對(duì)象都得到通知并自動(dòng)更新”。回想一下OGRE的消息機(jī)制,用的正是該模式。

                  為了得到OGRE的各種消息(更新、鼠標(biāo)、鍵盤),在初始化EventProcessor后需要向它添加各種ListenersKeyListenerMouseListenerMouseMotionListener。而EventProcessor本身又是個(gè)FrameListener,在它startProcessingEvents的時(shí)候,又以FrameListener的身份注冊(cè)到Root中。可以看出,Root是消息的發(fā)布者EventProcessor 是個(gè)代理,它把消息分發(fā)給各種訂閱者KeyListenerMouseListenerMouseMotionListener

            至于消息是如何分發(fā)的,可以參考Chain of Responsibility模式或消息機(jī)制分析。

             

            十、Strategy

            Strategy模式在于實(shí)現(xiàn)算法與使用它的客戶之間的分離,使得算法可以獨(dú)立的變化。

            回想一下Bridge模式,可以發(fā)現(xiàn),兩者之間有些相似性:使得某一部分可以獨(dú)立的變化。只不過Bridge是將抽象部分與它的實(shí)現(xiàn)部分分離。從兩者所屬的類別來看,Bridge強(qiáng)調(diào)靜態(tài)結(jié)構(gòu),而Strategy強(qiáng)調(diào)更多的是行為——算法的獨(dú)立性。

            同樣是Bridge模式中的例子,若把Mesh各版本文件讀取的實(shí)現(xiàn)看作是算法,把MeshSerializer看作是算法的客戶,那么該例也可以看作是Strategy模式。具體參考Bridge模式。

            從上面可以看出,模式之間本沒有絕對(duì)的界限,從不同的角度看可以得到不同的結(jié)論;另一方面,模式的實(shí)現(xiàn)也是隨機(jī)應(yīng)變,要與具體的問題想結(jié)合。

             

            十一、Template Method

                  Template Method比較簡(jiǎn)單的一個(gè)模式,屬于類行為模式。可以用“全局與細(xì)節(jié)”、“步驟與實(shí)現(xiàn)”來概括,具體就是基類定義全局和步驟,子類來實(shí)現(xiàn)每一步的細(xì)節(jié)。

                  OGRE給的Example框架使用了該模式,并具代表性。看一下ExampleApplicationsetup()成員:

                  bool setup(void)
                
            {
                    mRoot = 
            new Root();

                    setupResources();

                    
            bool carryOn = configure();
                    
            if (!carryOn) return false;

                    chooseSceneManager();
                    createCamera();
                    createViewports();

                    
            // Set default mipmap level (NB some APIs ignore this)
                    TextureManager::getSingleton().setDefaultNumMipmaps(5);

                    
            // Create any resource listeners (for loading screens)
                    createResourceListener();
                    
            // Load resources
                    loadResources();

                    
            // Create the scene
                    createScene();

                    createFrameListener();

                    
            return true;

                }

             

                  該成員函數(shù)調(diào)用的其他virtual成員函數(shù)都有默認(rèn)的實(shí)現(xiàn),若不滿足需求,子類可以自行實(shí)現(xiàn)。而setup()只是定義了一個(gè)設(shè)置順序。

             

            posted on 2005-12-14 11:26 力為 閱讀(3956) 評(píng)論(4)  編輯 收藏 引用 所屬分類: 7. OGRE Analysis

            評(píng)論

            # re: OGRE分析之設(shè)計(jì)模式(四) 2006-09-11 12:12 天歌
            寫得很不錯(cuò),頂
            我想學(xué)OGre+設(shè)計(jì)模式 經(jīng)典
            感謝阿!!!  回復(fù)  更多評(píng)論
              

            # re: OGRE分析之設(shè)計(jì)模式(四) 2007-02-03 03:22 rhett
            你的文章寫的很多,讓我對(duì)ogre的理解更進(jìn)一步,希望你能繼續(xù)   回復(fù)  更多評(píng)論
              

            # re: OGRE分析之設(shè)計(jì)模式(四) 2007-07-09 11:29 Lucien
            此章內(nèi)容不錯(cuò),全部看完,力為繼續(xù)加油!  回復(fù)  更多評(píng)論
              

            # re: OGRE分析之設(shè)計(jì)模式(四) 2008-11-24 10:22 lxzsh2000
            非常好,感謝  回復(fù)  更多評(píng)論
              

            色欲av伊人久久大香线蕉影院| 久久久久亚洲精品无码网址 | 久久人人妻人人爽人人爽| 久久天天躁夜夜躁狠狠| 久久中文骚妇内射| 久久99热这里只有精品国产| 久久国产亚洲精品| 国内精品久久国产大陆| 国内精品伊人久久久影院| 99久久精品毛片免费播放| 亚洲欧美国产精品专区久久| 久久久无码人妻精品无码| 亚洲国产成人精品无码久久久久久综合 | 国产麻豆精品久久一二三| 思思久久99热免费精品6| 久久久久久亚洲AV无码专区| 久久午夜综合久久| 国内精品久久久久久野外| 97久久国产露脸精品国产| 久久精品国产福利国产琪琪| 精品久久久久香蕉网| 国色天香久久久久久久小说| 久久久久亚洲?V成人无码| 久久综合久久自在自线精品自| 亚洲国产精品嫩草影院久久| 亚洲国产成人久久精品动漫| 99国产欧美久久久精品蜜芽| 一本色道久久综合狠狠躁| 中文字幕精品久久久久人妻| 久久精品成人免费观看97| 狠狠久久亚洲欧美专区| 成人国内精品久久久久一区| 麻豆AV一区二区三区久久 | 久久99精品国产麻豆宅宅| 国产午夜精品久久久久九九电影 | 久久青青国产| 欧美久久综合九色综合| 免费久久人人爽人人爽av| 久久久久免费精品国产| 一本一本久久aa综合精品| 久久综合国产乱子伦精品免费|