力为的技术博客

联系 聚合 管理
  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;对关联式容器(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模式“定义对象间一对多的依赖关系,当一个对象的状态发生变化时,所有依赖他的对象都得到通知并自动更新”。回想一下OGRE的消息机制,用的正是该模式。

      为了得到OGRE的各种消息(更新、鼠标、键盘),在初始化EventProcessor后需要向它添加各种ListenersKeyListenerMouseListenerMouseMotionListener。而EventProcessor本身又是个FrameListener,在它startProcessingEvents的时候,又以FrameListener的身份注册到Root中。可以看出,Root是消息的发布者EventProcessor 是个代理,它把消息分发给各种订阅者KeyListenerMouseListenerMouseMotionListener

至于消息是如何分发的,可以参考Chain of Responsibility模式或消息机制分析。

 

十、Strategy

Strategy模式在于实现算法与使用它的客户之间的分离,使得算法可以独立的变化。

回想一下Bridge模式,可以发现,两者之间有些相似性:使得某一部分可以独立的变化。只不过Bridge是将抽象部分与它的实现部分分离。从两者所属的类别来看,Bridge强调静态结构,而Strategy强调更多的是行为——算法的独立性。

同样是Bridge模式中的例子,若把Mesh各版本文件读取的实现看作是算法,把MeshSerializer看作是算法的客户,那么该例也可以看作是Strategy模式。具体参考Bridge模式。

从上面可以看出,模式之间本没有绝对的界限,从不同的角度看可以得到不同的结论;另一方面,模式的实现也是随机应变,要与具体的问题想结合。

 

十一、Template Method

      Template Method比较简单的一个模式,属于类行为模式。可以用“全局与细节”、“步骤与实现”来概括,具体就是基类定义全局和步骤,子类来实现每一步的细节。

      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;

    }

 

      该成员函数调用的其他virtual成员函数都有默认的实现,若不满足需求,子类可以自行实现。而setup()只是定义了一个设置顺序。

 

posted on 2005-12-14 11:26 力为 阅读(3921) 评论(4)  编辑 收藏 引用 所属分类: 7. OGRE Analysis

评论

# re: OGRE分析之设计模式(四) 2006-09-11 12:12 天歌
写得很不错,顶
我想学OGre+设计模式 经典
感谢阿!!!  回复  更多评论
  

# re: OGRE分析之设计模式(四) 2007-02-03 03:22 rhett
你的文章写的很多,让我对ogre的理解更进一步,希望你能继续   回复  更多评论
  

# re: OGRE分析之设计模式(四) 2007-07-09 11:29 Lucien
此章内容不错,全部看完,力为继续加油!  回复  更多评论
  

# re: OGRE分析之设计模式(四) 2008-11-24 10:22 lxzsh2000
非常好,感谢  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理