Skip to content

Commit 13c9f99

Browse files
committed
add iterator and change uml before
1 parent 3707ac2 commit 13c9f99

34 files changed

+253
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
[解释器模式 Interpreter](doc/17-解释器.md)
4646

47-
迭代器模式 Iterator
47+
[迭代器模式 Iterator](doc/18-迭代器.md)
4848

4949
中介者模式 Mediator
5050

code/17_iterator/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
PROJECT(iterator_test)
3+
SET(SRC_LIST client.cpp)
4+
SET(PATTERN_SRC iterator_types.cpp)
5+
6+
include_directories(./)
7+
8+
ADD_EXECUTABLE(iterator_test ${SRC_LIST} ${PATTERN_SRC})

code/17_iterator/client.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <iostream>
2+
3+
#include "iterator_types.h"
4+
5+
void mysleep(int count);
6+
7+
int main(){
8+
std::cout<<"Iterator pattern case:"<<std::endl;
9+
std::vector<Post*> list;
10+
list.push_back(new Post("Post 1"));
11+
list.push_back(new Post("Post 2"));
12+
list.push_back(new Post("Post 3"));
13+
list.push_back(new Post("Post 4"));
14+
list.push_back(new Post("Post 5"));
15+
16+
PostBox* box = new PostBox(list);
17+
Iterator* iter = box->createIterator();
18+
19+
while(iter->hasNext()){
20+
box->switchAndShow(iter->next());
21+
mysleep(10000);
22+
}
23+
24+
return 0;
25+
}
26+
27+
void mysleep(int count){
28+
for(int i=0;i<count;i++){
29+
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "iterator_types.h"
2+
3+
PostIterator::PostIterator(PostBox* bx):box(bx),index(0),total(1){
4+
if(bx){
5+
this->total = bx->totalSize();
6+
}
7+
}
8+
9+
int PostIterator::next(){
10+
int temp = this->index;
11+
this->index = (++this->index) % this->total;
12+
return temp;
13+
}
14+
15+
bool PostIterator::hasNext(){
16+
17+
return true;
18+
}
19+
20+
// PostBox
21+
Iterator* PostBox::createIterator(){
22+
23+
return new PostIterator(this);
24+
}
25+
26+
void PostBox::switchAndShow(int index){
27+
this->postList.at(index)->show();
28+
}
29+
30+
int PostBox::totalSize(){
31+
return this->postList.size();
32+
}

code/17_iterator/iterator_types.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#pragma once
2+
#include <iostream>
3+
#include <string>
4+
#include <vector>
5+
6+
class Iterator{
7+
public:
8+
virtual int next()=0;
9+
virtual bool hasNext()=0;
10+
};
11+
12+
class Aggregate{
13+
public:
14+
virtual Iterator* createIterator()=0;
15+
};
16+
17+
class Post{
18+
public:
19+
Post(std::string cont):m_content(cont){
20+
21+
}
22+
void show(){
23+
std::cout<<m_content<<std::endl;
24+
}
25+
private:
26+
std::string m_content;
27+
};
28+
29+
class PostBox;
30+
class PostIterator: public Iterator{
31+
public:
32+
PostIterator(PostBox* bx);
33+
int next();
34+
bool hasNext();
35+
private:
36+
PostBox* box;
37+
int index;
38+
int total;
39+
};
40+
41+
42+
43+
class PostBox: public Aggregate{
44+
public:
45+
PostBox(std::vector<Post*> list):postList(list){
46+
47+
}
48+
Iterator* createIterator();
49+
void switchAndShow(int index);
50+
int totalSize();
51+
private:
52+
std::vector<Post*> postList;
53+
};

doc/18-迭代器.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 迭代器
2+
3+
提供一个对象来顺序访问聚合对象中的一系列数据,而不暴露聚合对象的内部表示
4+
5+
6+
## UML
7+
8+
* 抽象迭代器(Iterator)角色:定义访问和遍历聚合元素的接口,通常包含 hasNext()、first()、next() 等方法。
9+
* 具体迭代器(ConcreteIterator)角色:实现抽象迭代器接口中所定义的方法,完成对聚合对象的遍历,记录遍历的当前位置
10+
* 抽象聚合(Aggregate)角色:定义存储、添加、删除聚合对象以及创建迭代器对象的接口。
11+
* 具体聚合(ConcreteAggregate)角色:实现抽象聚合类,返回一个具体迭代器的实例。
12+
13+
![figure17_iterator](img/figure17_iterator.png)
14+
15+
## 优缺点
16+
17+
* 优点
18+
* 访问一个聚合对象的内容而无须暴露它的内部表示
19+
* 遍历任务交由迭代器完成,这简化了聚合类
20+
* 它支持以不同方式遍历一个聚合,甚至可以自定义迭代器的子类以支持新的遍历
21+
* 迭代器模式中引入抽象层,易于增加新的迭代器类,便于扩展,符合开闭原则
22+
23+
24+
* 缺点
25+
* 增加了类的个数,这在一定程度上增加了系统的复杂性
26+
27+
28+
## 使用场景
29+
30+
* 访问一个聚合对象而无需暴露它的内部结构
31+
* 需要为一个聚合对象提供多种遍历方法
32+
33+
## 用例
34+
35+
现在机械可滚动的广告箱生活中还是能见到,比如公交站台上可滚动的广告箱;
36+
用户可以看到上面展示的广告宣传画布每隔一定时间滚动切换至下一幅广告. 用户并不关心这些广告是怎么存储以及结构是啥, 用户关注的是周期展示的广告画.
37+
38+
![figure17_iterator_case](img/figure17_iterator_case.png)
39+
40+
[code](../code/17_iterator)

doc/img/figure10_decorator.png

1.08 KB
Loading

doc/img/figure12_flyweight.png

125 Bytes
Loading

doc/img/figure13_proxy.png

108 Bytes
Loading

doc/img/figure14_chain.png

302 Bytes
Loading

0 commit comments

Comments
 (0)