• <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
            非常好,感謝  回復  更多評論
              

            一本一本久久aa综合精品 | 99精品伊人久久久大香线蕉| 四虎国产精品成人免费久久| 欧美日韩精品久久久久| 久久久久人妻精品一区三寸蜜桃| 婷婷久久综合九色综合九七| 亚洲精品无码成人片久久| 99精品国产99久久久久久97| 亚洲色欲久久久久综合网| 7777精品久久久大香线蕉| 亚洲国产成人久久精品99 | 久久99国产精品成人欧美| 中文字幕久久精品| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 久久精品极品盛宴观看| 亚洲va中文字幕无码久久 | 国产精品岛国久久久久| 女同久久| 青青国产成人久久91网| 亚洲欧洲日产国码无码久久99| 久久99这里只有精品国产| 久久人人爽人人爽人人片AV高清| 伊人久久综合热线大杳蕉下载| 99久久国产亚洲高清观看2024| 久久se精品一区精品二区国产 | 久久综合一区二区无码| 久久精品久久久久观看99水蜜桃| 午夜久久久久久禁播电影| 久久久中文字幕| 一级a性色生活片久久无| 亚洲色大成网站WWW久久九九| avtt天堂网久久精品| 久久香综合精品久久伊人| 日韩人妻无码精品久久久不卡| 久久亚洲视频| 久久精品国产亚洲AV无码娇色 | 99久久成人国产精品免费| 人人狠狠综合88综合久久| 97久久超碰国产精品旧版| 久久久久亚洲AV综合波多野结衣| 亚洲欧洲日产国码无码久久99|