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

            力為的技術博客

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

                                                                                      DownLoad
            OGRE
            分析之設計模式(四)

            Mythma

             Email: mythma@163.com

                  OGRE的設計結構十分清晰,這得歸功于設計模式的成功運用。

            八、Iterator

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

            OGRE的包裝本身沒有什么復雜,看一下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模式“定義對象間一對多的依賴關系,當一個對象的狀態(tài)發(fā)生變化時,所有依賴他的對象都得到通知并自動更新”?;叵胍幌?/SPAN>OGRE的消息機制,用的正是該模式。

                  為了得到OGRE的各種消息(更新、鼠標、鍵盤),在初始化EventProcessor后需要向它添加各種ListenersKeyListener、MouseListenerMouseMotionListener。而EventProcessor本身又是個FrameListener,在它startProcessingEvents的時候,又以FrameListener的身份注冊到Root中??梢钥闯觯?/SPAN>Root是消息的發(fā)布者,EventProcessor 是個代理,它把消息分發(fā)給各種訂閱者KeyListener、MouseListenerMouseMotionListener

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

             

            十、Strategy

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

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

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

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

             

            十一、Template Method

                  Template Method比較簡單的一個模式,屬于類行為模式??梢杂谩叭峙c細節(jié)”、“步驟與實現(xiàn)”來概括,具體就是基類定義全局和步驟,子類來實現(xiàn)每一步的細節(jié)。

                  OGRE給的Example框架使用了該模式,并具代表性??匆幌?/SPAN>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ù)調用的其他virtual成員函數(shù)都有默認的實現(xiàn),若不滿足需求,子類可以自行實現(xiàn)。而setup()只是定義了一個設置順序。

             

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

            評論

            # re: OGRE分析之設計模式(四) 2006-09-11 12:12 天歌
            寫得很不錯,頂
            我想學OGre+設計模式 經典
            感謝阿!!!  回復  更多評論
              

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

            # re: OGRE分析之設計模式(四) 2007-07-09 11:29 Lucien
            此章內容不錯,全部看完,力為繼續(xù)加油!  回復  更多評論
              

            # re: OGRE分析之設計模式(四) 2008-11-24 10:22 lxzsh2000
            非常好,感謝  回復  更多評論
              

            男女久久久国产一区二区三区| 一本一本久久A久久综合精品| 精品国产青草久久久久福利| 亚洲国产成人精品91久久久 | 久久大香香蕉国产| 久久精品国产91久久麻豆自制| 久久国产精品免费一区二区三区 | 国产亚洲色婷婷久久99精品| 国产巨作麻豆欧美亚洲综合久久| 欧美伊人久久大香线蕉综合| 国产精品禁18久久久夂久| 九九热久久免费视频| 久久久久久久亚洲Av无码| 老司机午夜网站国内精品久久久久久久久 | 精品国产91久久久久久久a| 亚洲精品乱码久久久久久按摩 | 久久99热只有频精品8| 午夜视频久久久久一区| 青青青伊人色综合久久| 精产国品久久一二三产区区别 | 久久午夜夜伦鲁鲁片免费无码影视| 久久久久亚洲AV成人片 | 99久久无码一区人妻| 精品少妇人妻av无码久久| 久久天天躁夜夜躁狠狠| 久久人妻少妇嫩草AV蜜桃| 久久免费视频网站| 国产精品无码久久久久久| 国产成人精品综合久久久| 亚洲人成电影网站久久| 久久免费香蕉视频| 香蕉aa三级久久毛片| 性高朝久久久久久久久久| 人妻少妇精品久久| 精品国产乱码久久久久软件| 亚洲国产精品无码久久久久久曰 | 久久精品国产亚洲av水果派| 伊人色综合久久天天人手人婷 | 久久99国产精品99久久| 97久久香蕉国产线看观看| 久久婷婷综合中文字幕|