|
38 | 38 |  |
39 | 39 |
|
40 | 40 | [code](../code/17_iterator) |
| 41 | + |
| 42 | + |
| 43 | +```c++ |
| 44 | +// iterator_types.h |
| 45 | + |
| 46 | +#pragma once |
| 47 | +#include <iostream> |
| 48 | +#include <string> |
| 49 | +#include <vector> |
| 50 | + |
| 51 | +class Iterator{ |
| 52 | +public: |
| 53 | + virtual int next()=0; |
| 54 | + virtual bool hasNext()=0; |
| 55 | +}; |
| 56 | + |
| 57 | +class Aggregate{ |
| 58 | +public: |
| 59 | + virtual Iterator* createIterator()=0; |
| 60 | +}; |
| 61 | + |
| 62 | +class Post{ |
| 63 | +public: |
| 64 | + Post(std::string cont):m_content(cont){ |
| 65 | + |
| 66 | + } |
| 67 | + void show(){ |
| 68 | + std::cout<<m_content<<std::endl; |
| 69 | + } |
| 70 | +private: |
| 71 | + std::string m_content; |
| 72 | +}; |
| 73 | + |
| 74 | +class PostBox; |
| 75 | +class PostIterator: public Iterator{ |
| 76 | +public: |
| 77 | + PostIterator(PostBox* bx); |
| 78 | + int next(); |
| 79 | + bool hasNext(); |
| 80 | +private: |
| 81 | + PostBox* box; |
| 82 | + int index; |
| 83 | + int total; |
| 84 | +}; |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +class PostBox: public Aggregate{ |
| 89 | +public: |
| 90 | + PostBox(std::vector<Post*> list):postList(list){ |
| 91 | + |
| 92 | + } |
| 93 | + Iterator* createIterator(); |
| 94 | + void switchAndShow(int index); |
| 95 | + int totalSize(); |
| 96 | +private: |
| 97 | + std::vector<Post*> postList; |
| 98 | +}; |
| 99 | +``` |
| 100 | + |
| 101 | +```c++ |
| 102 | +// iterator_types.cpp |
| 103 | +#include "iterator_types.h" |
| 104 | + |
| 105 | +PostIterator::PostIterator(PostBox* bx):box(bx),index(0),total(1){ |
| 106 | + if(bx){ |
| 107 | + this->total = bx->totalSize(); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +int PostIterator::next(){ |
| 112 | + int temp = this->index; |
| 113 | + this->index = (++this->index) % this->total; |
| 114 | + return temp; |
| 115 | +} |
| 116 | + |
| 117 | +bool PostIterator::hasNext(){ |
| 118 | + |
| 119 | + return true; |
| 120 | +} |
| 121 | + |
| 122 | +// PostBox |
| 123 | +Iterator* PostBox::createIterator(){ |
| 124 | + |
| 125 | + return new PostIterator(this); |
| 126 | +} |
| 127 | + |
| 128 | +void PostBox::switchAndShow(int index){ |
| 129 | + this->postList.at(index)->show(); |
| 130 | +} |
| 131 | + |
| 132 | +int PostBox::totalSize(){ |
| 133 | + return this->postList.size(); |
| 134 | +} |
| 135 | +``` |
| 136 | +
|
| 137 | +```c++ |
| 138 | +// client.cpp |
| 139 | +#include <iostream> |
| 140 | +
|
| 141 | +#include "iterator_types.h" |
| 142 | +
|
| 143 | +void mysleep(long count); |
| 144 | +
|
| 145 | +int main(){ |
| 146 | + std::cout<<"Iterator pattern case:"<<std::endl; |
| 147 | + std::vector<Post*> list; |
| 148 | + list.push_back(new Post("Post 1")); |
| 149 | + list.push_back(new Post("Post 2")); |
| 150 | + list.push_back(new Post("Post 3")); |
| 151 | + list.push_back(new Post("Post 4")); |
| 152 | + list.push_back(new Post("Post 5")); |
| 153 | + |
| 154 | + const int POST_SIZE = list.size(); |
| 155 | + PostBox* box = new PostBox(list); |
| 156 | + Iterator* iter = box->createIterator(); |
| 157 | +
|
| 158 | + while(iter->hasNext()){ |
| 159 | + int postIndex = iter->next(); |
| 160 | + box->switchAndShow(postIndex); |
| 161 | + if(postIndex == (POST_SIZE-1)){ |
| 162 | + std::cout<< " --- " <<std::endl; |
| 163 | + } |
| 164 | + mysleep(50000000); |
| 165 | + } |
| 166 | +
|
| 167 | + return 0; |
| 168 | +} |
| 169 | +
|
| 170 | +void mysleep(long count){ |
| 171 | + |
| 172 | + for(long i=0;i<count;i++){ |
| 173 | + |
| 174 | + } |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | + |
0 commit comments