diff --git a/16.InterpreterPattern/2.Code/InterpreterPattern.h b/16.InterpreterPattern/2.Code/InterpreterPattern.h index dd008ae..744b8d7 100644 --- a/16.InterpreterPattern/2.Code/InterpreterPattern.h +++ b/16.InterpreterPattern/2.Code/InterpreterPattern.h @@ -1,5 +1,5 @@ -#ifndef __COMMAND_PATTERN_H__ -#define __COMMAND_PATTERN_H__ +#ifndef __INTERPRETOR_PATTERN_H__ +#define __INTERPRETOR_PATTERN_H__ #include using namespace std; @@ -126,4 +126,4 @@ private: }; -#endif //__COMMAND_PATTERN_H__ \ No newline at end of file +#endif //__INTERPRETOR_PATTERN_H__ \ No newline at end of file diff --git a/17.IteratorPattern/1.Picture/迭代器模式UML图.png b/17.IteratorPattern/1.Picture/迭代器模式UML图.png new file mode 100644 index 0000000..fc134bc Binary files /dev/null and b/17.IteratorPattern/1.Picture/迭代器模式UML图.png differ diff --git a/17.IteratorPattern/1.Picture/迭代器模式实例UML图.png b/17.IteratorPattern/1.Picture/迭代器模式实例UML图.png new file mode 100644 index 0000000..9fb7c5c Binary files /dev/null and b/17.IteratorPattern/1.Picture/迭代器模式实例UML图.png differ diff --git a/17.IteratorPattern/2.Code/Aggregate.cpp b/17.IteratorPattern/2.Code/Aggregate.cpp new file mode 100644 index 0000000..4fc5702 --- /dev/null +++ b/17.IteratorPattern/2.Code/Aggregate.cpp @@ -0,0 +1,21 @@ +#include "Iterator.h" + +Television::Television(){} + +Television::Television(vector iChannelList){ + this->channelList = iChannelList; +} + +Iterator* Television::createIterator(){ + RemoteControl *it = new RemoteControl(); + it->setTV(this); + return (Iterator*)it; +} + +int Television::getTotalChannelNum(){ + return channelList.size(); +} + +void Television::play(int i){ + printf("ڲţ%s\n", channelList[i].c_str()); +} \ No newline at end of file diff --git a/17.IteratorPattern/2.Code/Aggregate.h b/17.IteratorPattern/2.Code/Aggregate.h new file mode 100644 index 0000000..ac3f81f --- /dev/null +++ b/17.IteratorPattern/2.Code/Aggregate.h @@ -0,0 +1,34 @@ +#ifndef __AGGREGATE_H__ +#define __AGGREGATE_H__ + +#include +using namespace std; + +// ǰΪ໥ +class Iterator; +class RemoteControl; + +// ۺ Aggregate +class Aggregate +{ +public: + Aggregate(){} + virtual Iterator* createIterator() = 0; +}; + +// ۺ Television +class Television :public Aggregate +{ +public: + Television(); + Television(vector iChannelList); + // ʵִ + Iterator* createIterator(); + // ȡܵƵĿ + int getTotalChannelNum(); + void play(int i); +private: + vector channelList; +}; + +#endif //__AGGREGATE_H__ \ No newline at end of file diff --git a/17.IteratorPattern/2.Code/Iterator.h b/17.IteratorPattern/2.Code/Iterator.h new file mode 100644 index 0000000..b4e29bb --- /dev/null +++ b/17.IteratorPattern/2.Code/Iterator.h @@ -0,0 +1,69 @@ +#ifndef _ITERATOR_H_ +#define _ITERATOR_H_ + +#pragma once +#include "Aggregate.h" + +#include +using namespace std; + +// +class Iterator +{ +public: + 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 \ No newline at end of file diff --git a/17.IteratorPattern/2.Code/main.cpp b/17.IteratorPattern/2.Code/main.cpp new file mode 100644 index 0000000..b91d5b3 --- /dev/null +++ b/17.IteratorPattern/2.Code/main.cpp @@ -0,0 +1,35 @@ +#include +#include "Iterator.h" + +int main() +{ + vector channelList = { "Ƶ", "ƾƵ", "Ƶ", "ӰƵ", "Ƶ", "ũҵƵ", "Ĵ", "ɶ" }; + // + Television *tv = new Television(channelList); + // ң + Iterator *remoteControl = tv->createIterator(); + + // ˳ + printf("˳:\n"); + remoteControl->first(); + // Ƶ + while (remoteControl->hasNext()){ + remoteControl->currentChannel(); + remoteControl->next(); + } + + printf("\n\n"); + + // + printf(":\n"); + remoteControl->last(); + // Ƶ + while (remoteControl->hasPrevious()){ + remoteControl->currentChannel(); + remoteControl->previous(); + } + + printf("\n\n"); + system("pause"); + return 0; +} \ No newline at end of file diff --git a/README.md b/README.md index 12adce3..08ce255 100644 --- a/README.md +++ b/README.md @@ -77,4 +77,8 @@ Jungle设计模式系列 19.设计模式(十九)——解释器模式 -博客地址:https://blog.csdn.net/sinat_21107433/article/details/102864850 \ No newline at end of file +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102864850 + +20.设计模式(二十)——迭代器模式 + +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102879383 \ No newline at end of file