Chinaunix首页 | 论坛 | 博客
  • 博客访问: 167045
  • 博文数量: 71
  • 博客积分: 165
  • 博客等级: 入伍新兵
  • 技术积分: 431
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-17 00:35
文章分类
文章存档

2013年(3)

2012年(68)

分类:

2012-11-09 13:22:08

 iterator.h

点击(此处)折叠或打开

  1. /*
  2.  * iterator.h
  3.  *
  4.  * Created on: Sep 29, 2012
  5.  * Author: hl
  6.  */

  7. #ifndef _ITERATOR_H_
  8. #define _ITERATOR_H_
  9. #include <string>
  10. #include <vector>
  11. using namespace std;

  12. /**
  13.  * @brief 迭代器模式
  14.  */
  15. class IProject
  16. {
  17. public:
  18.     virtual void show() = 0;
  19. };

  20. class Project : public IProject
  21. {
  22. public:
  23.     Project(string proName, double money, string time);
  24. public:
  25.     void show();
  26. private:
  27.     string _proName;
  28.     double _money;
  29.     string _time;
  30. };

  31. class ProIterator;

  32. /**
  33.  * @brief 项目管理器
  34.  */
  35. class ProManager
  36. {
  37. public:
  38.     ProManager();
  39.     ~ProManager();
  40. public:
  41.     void proAdd(string proName, double money, string time);
  42.     ProIterator * getIterator();            /// < 得到对象的遍历器
  43. private:
  44.     vector<Project * > * vec;
  45. };

  46. /**
  47.  * @brief 项目遍历器
  48.  */
  49. class ProIterator
  50. {
  51. public:
  52.     ProIterator(vector<Project * > *vec);
  53.     ~ProIterator();
  54. public:
  55.     bool hasNext();            /// < 是否有下一个!
  56.     Project * next();        /// <
  57.     void erase();            /// < 遍历一个,释放一个,并且重新组织新的遍历器!
  58. private:
  59.     vector<Project * > * _vec;
  60.     vector<Project * >::iterator iter;
  61. };

  62. #endif /* _ITERATOR_H_ */
iterator.cpp

点击(此处)折叠或打开

  1. //============================================================================
  2. // Name : iterator.cpp
  3. // Author : hl
  4. // Version :
  5. // Copyright : Copyright (c) 2012 Tiros
  6. // Description : Iterator Pattern in C++, Ansi-style
  7. //============================================================================

  8. #include "iterator.h"
  9. #include <iostream>
  10. using namespace std;


  11. Project::Project(string proName, double money, string time)
  12. {
  13.     this->_proName = proName;
  14.     this->_money = money;
  15.     this->_time = time;
  16. }

  17. void Project::show()
  18. {
  19.     cout << "项目名称: " << this->_proName << " 项目资金: "
  20.             << this->_money << " 项目启动时间: " << this->_time;
  21. }

  22. ProManager::ProManager()
  23. {
  24.     this->vec = new vector<Project * >;
  25.     if (!this->vec)
  26.         return ;
  27. }

  28. ProManager::~ProManager()
  29. {
  30.     //TODO
  31. }

  32. void ProManager::proAdd(string proName, double money, string time)
  33. {
  34.     Project * pro = new Project(proName, money, time);
  35.     this->vec->push_back(pro);
  36. }

  37. ProIterator * ProManager::getIterator()
  38. {
  39.     return (new ProIterator(this->vec));
  40. }

  41. ProIterator::ProIterator(vector<Project * > *vec)
  42. {
  43.     if (!vec)
  44.         return ;
  45.     this->_vec = vec;
  46.     this->iter = this->_vec->begin();
  47. }

  48. ProIterator::~ProIterator()
  49. {
  50.     //TODO
  51. }

  52. bool ProIterator::hasNext()
  53. {
  54.     if (0 == this->_vec->size())
  55.         return false;
  56.     return true;
  57. }

  58. Project * ProIterator::next()
  59. {
  60.     if ( 0 == this->_vec->size())
  61.         return NULL;
  62.     return *(this->iter);
  63. }

  64. void ProIterator::erase()
  65. {
  66.     this->_vec->erase(this->iter);
  67.     this->iter = this->_vec->begin();    /// < 释放之后重新调整迭代器
  68. }

  69. string getTimeStr()
  70. {
  71.     time_t timep;

  72.     time(&timep); /*获取time_t类型的当前时间*/
  73.     /*用gmtime将time_t类型的时间转换为struct tm类型的时间按,//没有经过时区转换的UTC时间
  74.      然后再用asctime转换为我们常见的格式 Fri Jan 11 17:25:24 2012
  75.      1> asctime(gmtime(&timep))
  76.      2> ctime(&timep) //经过时区转换的
  77.      */
  78.     return ctime(&timep);
  79. }

  80. int main()
  81. {
  82.     ProManager pro;
  83.     pro.proAdd("计划书", 300000.00, getTimeStr());
  84.     pro.proAdd("分布式", 400000.00, getTimeStr());
  85.     pro.proAdd("ERP", 600000.00, getTimeStr());

  86.     ProIterator * pProIterator = pro.getIterator();
  87.     while (pProIterator->hasNext())
  88.     {
  89.         Project * tp = pProIterator->next();
  90.         tp->show();
  91.         pProIterator->erase();
  92.     }
  93.     delete pProIterator;

  94.     return 0;
  95. }

阅读(530) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~