Iterator.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * C++ Design Patterns: ITERATOR
  3. *
  4. * http://www.plantuml.com/plantuml/uml/VP71RjD048Rl-nGZzP1MKOYuHfRL1ZZa0gY41vXuf-wYzMoPdQqQeE_2aJo7NuoDWqrIHzsJv_-_yJ-ZkOwAegdnPZw1rVh3xStwqorCvYQ_ocECyCuxOZN6iPBSOKLmKzT2DIh1JmFvNK0bbCjL1b23N5wPvw4xS3QLNlPCHpsmsCDAJ_eFg4cpstJEYe_KB-u7NLujuAiRLBIsPuU_F1_FtESC8tD6Jl0Bk7CIJxK3wkBxmDGPon1yltCI8TPHx6Zr5z7JSVH1FXy_FHi_VIt-tba2QXvq2mqr6v9epE6AC9kL_yztBFP3ci65ujTiDhwVwSbJV-Wt79G08tnysqrQRCgY2Zx8Gi8M_T8dAdkuc7TEMSmtPG-ljm-qs0kmf7jyT47miF9daeOKx0IhgjrP4i3q13OmjpkAMKyCM4kxgr6x1alG8BlsZn34TlSz4QiZxtE5704VWnFeoFOtL84PdMRK8qILL6dAXQMZXEnDmkORwHj3R66_gBdENwdnVm40http://www.plantuml.com/plantuml/uml/VP71RjD048Rl-nGZzP1MKOYuHfRL1ZZa0gY41vXuf-wYzMoPdQqQeE_2aJo7NuoDWqrIHzsJv_-_yJ-ZkOwAegdnPZw1rVh3xStwqorCvYQ_ocECyCuxOZN6iPBSOKLmKzT2DIh1JmFvNK0bbCjL1b23N5wPvw4xS3QLNlPCHpsmsCDAJ_eFg4cpstJEYe_KB-u7NLujuAiRLBIsPuU_F1_FtESC8tD6Jl0Bk7CIJxK3wkBxmDGPon1yltCI8TPHx6Zr5z7JSVH1FXy_FHi_VIt-tba2QXvq2mqr6v9epE6AC9kL_yztBFP3ci65ujTiDhwVwSbJV-Wt79G08tnysqrQRCgY2Zx8Gi8M_T8dAdkuc7TEMSmtPG-ljm-qs0kmf7jyT47miF9daeOKx0IhgjrP4i3q13OmjpkAMKyCM4kxgr6x1alG8BlsZn34TlSz4QiZxtE5704VWnFeoFOtL84PdMRK8qILL6dAXQMZXEnDmkORwHj3R66_gBdENwdnVm40
  5. * 2021 / JMB
  6. *
  7. */
  8. #include <iostream>
  9. #include <vector>
  10. class Iterator;
  11. class ConcreteIterator;
  12. class Aggregate {
  13. public:
  14. virtual ~Aggregate() {}
  15. virtual Iterator* createIterator() = 0;
  16. };
  17. class ConcreteAggregate : public Aggregate {
  18. public:
  19. ConcreteAggregate(int N) {
  20. //do a memory allocation
  21. v.reserve(N);
  22. for (int i = 0; i < N; ++i) {
  23. //direct move in memory of i value
  24. v.emplace_back(i);
  25. }
  26. }
  27. ~ConcreteAggregate() {}
  28. Iterator* createIterator();
  29. int getItem(int idx) { return v.at(idx); }
  30. unsigned int getSize() { return v.size(); }
  31. //emplace_back ==> move in memory
  32. void addItem(int obj) { v.emplace_back(obj); }
  33. private:
  34. std::vector<int> v;
  35. };
  36. class Iterator {
  37. public:
  38. virtual ~Iterator() {}
  39. virtual void first() = 0;
  40. virtual void next() = 0;
  41. virtual bool isDone() = 0;
  42. virtual int currentItem() = 0;
  43. };
  44. class ConcreteIterator : public Iterator {
  45. public:
  46. ConcreteIterator(ConcreteAggregate *ag)
  47. : p_cag(ag), p_index(0) {}
  48. void first() {
  49. p_index = 0;
  50. }
  51. void next() {
  52. ++p_index;
  53. }
  54. bool isDone() {
  55. return p_index >= p_cag->getSize();
  56. }
  57. int currentItem() {
  58. return p_cag->getItem(p_index);
  59. }
  60. private:
  61. ConcreteAggregate *p_cag;
  62. int p_index;
  63. };
  64. Iterator *ConcreteAggregate::createIterator() {
  65. std::cout << "createIterator" << std::endl;
  66. return new ConcreteIterator(this);
  67. }
  68. int main() {
  69. ConcreteAggregate *concreteAggregate = new ConcreteAggregate(10);
  70. concreteAggregate->addItem(123);
  71. Iterator* it = concreteAggregate->createIterator();
  72. for ( ; !it->isDone(); it->next()) {
  73. std::cout << "Item value: " << it->currentItem() << std::endl;
  74. }
  75. }