-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathIterator.h
70 lines (63 loc) · 1.05 KB
/
Iterator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef _ITERATOR_H_
#define _ITERATOR_H_
#pragma once
#include "Aggregate.h"
#include <vector>
using namespace std;
// 抽象迭代器
class Iterator
{
public:
Iterator(){}
virtual ~Iterator(){}
// 声明抽象遍历方法
virtual void first() = 0;
virtual void last() = 0;
virtual void next() = 0;
virtual void previous() = 0;
virtual bool hasNext() = 0;
virtual bool hasPrevious() = 0;
virtual void currentChannel() = 0;
private:
};
// 遥控器:具体迭代器
class RemoteControl :public Iterator
{
public:
RemoteControl(){}
void setTV(Television *iTv){
this->tv = iTv;
cursor = -1;
totalNum = tv->getTotalChannelNum();
}
// 实现各个遍历方法
void first(){
cursor = 0;
}
void last(){
cursor = totalNum - 1;
}
void next(){
cursor++;
}
void previous(){
cursor--;
}
bool hasNext(){
return !(cursor == totalNum);
}
bool hasPrevious(){
return !(cursor == -1);
}
void currentChannel(){
tv->play(cursor);
}
private:
// 游标
int cursor;
// 总的频道数目
int totalNum;
// 电视
Television* tv;
};
#endif