接上回:
http://www.csdn.net/develop/read_article.asp?id=11677
使用iterator模式,让每个队员做iterator,每个位置做aggregate,有如下几个好处:1.每个位置支持不同的队员用不同方式遍历。2.简化了每个位置的接口,使其kiss,不用具有遍历的接口。3.在同一个位置支持多个队员同时遍历。代码如下:template <class Item>class 位置{public: 位置(long size=缺省_位置_容量); long 计数() const; Item& Get(long index) const; //...};
template <class Item>class 队员{public: virtual void 回到初始位置()=0; virtual void 下一个位置()=0; virtual bool 是否到达() const=0; virtual Item 当前Item() const=0;protected: 队员();};
template <class Item>class 左后卫:public 队员<Item>{public: 左后卫(const 位置<Item>* 一个位置); virtual void 回到初始位置; virtual void 下一个位置(); virtual bool 是否到达() const; virtual Item 当前Item() const;private: const 位置<Item>* _位置; long _当前;};
关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放 / 友情链接 Copyright ©2001-2006 Lihuasoft.net webmaster(at)lihuasoft.net 网站编程QQ群 京ICP备05001064号 页面生成时间:0.00195