DesignPattern/17.IteratorPattern/2.Code/Iterator.h

70 lines
1.1 KiB
C
Raw Normal View History

2019-11-03 06:20:17 +00:00
#ifndef _ITERATOR_H_
#define _ITERATOR_H_
#pragma once
#include "Aggregate.h"
#include <vector>
using namespace std;
2021-10-28 15:15:51 +00:00
// 抽象迭代器
2019-11-03 06:20:17 +00:00
class Iterator
{
public:
Iterator(){}
2021-10-13 15:10:44 +00:00
virtual ~Iterator(){}
2021-10-28 15:15:51 +00:00
// 声明抽象遍历方法
2019-11-03 06:20:17 +00:00
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:
};
2021-10-28 15:15:51 +00:00
// 遥控器:具体迭代器
2019-11-03 06:20:17 +00:00
class RemoteControl :public Iterator
{
public:
RemoteControl(){}
void setTV(Television *iTv){
this->tv = iTv;
cursor = -1;
totalNum = tv->getTotalChannelNum();
}
2021-10-28 15:15:51 +00:00
// 实现各个遍历方法
2019-11-03 06:20:17 +00:00
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:
2021-10-28 15:15:51 +00:00
// 游标
2019-11-03 06:20:17 +00:00
int cursor;
2021-10-28 15:15:51 +00:00
// 总的频道数目
2019-11-03 06:20:17 +00:00
int totalNum;
2021-10-28 15:15:51 +00:00
// 电视
2019-11-03 06:20:17 +00:00
Television* tv;
};
#endif