OGRE分析之设计模式(四)

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()只是定义了一个设置顺序。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值