日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

设计模式复习-迭代器模式

發布時間:2025/6/17 asp.net 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式复习-迭代器模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Iterator.H #pragma once #include <list> #include <windows.h> using namespace std;/* 設計模式-迭代器模式(Iterator) 提供一種方法順序訪問一個聚合對象中的各個元素,而不暴露該對象內部表示。 (現在好多語言都已經內置實現了這個功能了,所以實際用途不大, 但是建議寫一下,實現過程價值遠遠大于使用價值。) */class Iterator{//迭代器抽象類public:virtual void * First() = 0;virtual void * Next() = 0;virtual BOOL IsDone() = 0;virtual void * CurrentItem() = 0; };class CAggregate {//聚集抽象類public:virtual Iterator * CreateIterator() = 0;virtual void Insert(void * const pNode) = 0;virtual void Remove(void * const pNode) = 0; };class CConcreteAggregate :public CAggregate {//具體聚集類public:list<void*>mpItems;CConcreteAggregate();void Clear();Iterator * CreateIterator();void Insert(void * const pNode);void Remove(void * const pNode); };class CConcreteIterator : public Iterator {//具體的迭代器類private:CConcreteAggregate *mpAggregate = NULL;int mnCurrent = 0; public:CConcreteIterator(CConcreteAggregate * pAggregate);void * First();void * Next();BOOL IsDone();void * CurrentItem(); };Iterator.CPP #include "stdafx.h" #include "Iterator.h"CConcreteAggregate::CConcreteAggregate() {mpItems.clear(); }void CConcreteAggregate::Clear() {for each(auto i in mpItems) {delete i;} }Iterator * CConcreteAggregate::CreateIterator() {return new CConcreteIterator(this); }void CConcreteAggregate::Insert(void * const pNode) {mpItems.push_back(pNode); } void CConcreteAggregate::Remove(void * const pNode) {mpItems.remove(pNode); }CConcreteIterator::CConcreteIterator(CConcreteAggregate * pAggregate) {mpAggregate = pAggregate;mnCurrent = 0; }void * CConcreteIterator::First() {return mpAggregate->mpItems.size() == 0 ? NULL :*(mpAggregate->mpItems.begin()); } void * CConcreteIterator::Next() {if (IsDone()) return NULL;int nSubscript = 0;mnCurrent++;for each(auto i in mpAggregate->mpItems) {if (nSubscript++ == mnCurrent + 1) {return i;}} } BOOL CConcreteIterator::IsDone() {return mnCurrent >= mpAggregate->mpItems.size(); } void * CConcreteIterator::CurrentItem() {int nSubscript = 0;for each(auto i in mpAggregate->mpItems) {if (nSubscript++ == mnCurrent) {return i;}}return NULL; }#pragma once #include "stdafx.h" #include "Iterator.h" #include<string> #include<iostream> using namespace std;int main() {CConcreteAggregate *pA = new CConcreteAggregate();pA->Insert(new string("node-1"));pA->Insert(new string("node-2"));string * pStr = new string("node-3");pA->Insert(pStr);Iterator *pIteratorA = new CConcreteIterator(pA);while (!pIteratorA->IsDone()) {cout << *((string*)pIteratorA->CurrentItem()) << endl;pIteratorA->Next();}pA->Remove(pStr);Iterator *pIteratorB = pA->CreateIterator();while (!pIteratorB->IsDone()) {cout << *((string*)pIteratorB->CurrentItem()) << endl;pIteratorB->Next();}pA->Clear();delete pIteratorA;delete pIteratorB;delete pA;delete pStr;getchar();return 0; }

總結

以上是生活随笔為你收集整理的设计模式复习-迭代器模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。