C#學(xué)習(xí)筆記整理-迭代器模式介紹
什么是迭代器模式?
迭代器模式(Iterator):提供一種方法順序訪問(wèn)一個(gè)聚合對(duì)象中各個(gè)元素,而又不暴露該對(duì)象的內(nèi)部表示。
何時(shí)使用迭代器模式?
當(dāng)需要訪問(wèn)一個(gè)聚合對(duì)象,而且不管這些對(duì)象是什么都需要遍歷的時(shí)候,需要考慮使用迭代器模式。
迭代器模式的組成
Iterator:迭代器抽象類(lèi),用于定義得到開(kāi)始對(duì)象,對(duì)到下一個(gè)對(duì)象,判斷是否到結(jié)尾,當(dāng)前對(duì)象等抽象方法,統(tǒng)一接口。
ConcreteAggregate:保存聚合對(duì)象。
ConcreteIterator:繼承于Iterator,實(shí)現(xiàn)具體如何對(duì)聚合對(duì)象的操作。
迭代器模式具體實(shí)現(xiàn)
迭代器模式的結(jié)構(gòu)
迭代器模式的實(shí)現(xiàn):
Iterator類(lèi):
abstract class Iterator
{
public abstract object First();
public abstract object Next();
public abstract bool IsDone();
public abstract object CurrentItem();
}
ConcreteIterator類(lèi):
//順序遍歷
class ConcreteIterator : Iterator
{
private ConcreteAggregate aggregate;
private int current = 0;
//將現(xiàn)在的數(shù)據(jù)組傳輸進(jìn)來(lái)
public ConcreteIterator(ConcreteAggregate aggregate)
{
this.aggregate = aggregate;
}
public override object CurrentItem()
{
return aggregate[current];
}
public override object First()
{
return aggregate[0];
}
public override bool IsDone()
{
return current >= aggregate.Count ? true : false;
}
public override object Next()
{
object obj = null;
current++;
if (current < aggregate.Count)
{
obj = aggregate[current];
}
return obj;
}
}
//逆序遍歷
class ConcreteIteratorDesc : Iterator
{
private ConcreteAggregate aggregate;
private int current = 0;
//傳輸數(shù)據(jù)進(jìn)來(lái)
public ConcreteIteratorDesc(ConcreteAggregate aggregate)
{
this.aggregate = aggregate;
current = aggregate.Count - 1;
}
public override object CurrentItem()
{
return aggregate[current];
}
public override object First()
{
return aggregate[aggregate.Count - 1];
}
public override bool IsDone()
{
return current < 0 ? true:false;
}
public override object Next()
{
object obj = null;
current--;
if (current >= 0)
{
obj = aggregate[current];
}
return obj;
}
}
ConcreteAggregate類(lèi):
/// <summary>
/// 創(chuàng)建迭代器
/// 在這里看并沒(méi)有什么具體的用處
/// </summary>
abstract class Aggregate
{
public abstract Iterator CreateIterator();
}
/// <summary>
/// 作用是保存數(shù)據(jù),保存的數(shù)據(jù)是一系列數(shù)據(jù),所以用數(shù)組
/// 然后傳輸數(shù)據(jù)給ConcreteIterator
/// </summary>
class ConcreteAggregate : Aggregate
{
//用于存放聚合對(duì)象
private IList<object> items = new List<object>();
public override Iterator CreateIterator()
{
return new ConcreteIterator(this);
}
//數(shù)組的長(zhǎng)度,也就是ConcreteAggregate的屬性
public int Count { get { return items.Count; } }
/// ConcreteAggregate現(xiàn)在是數(shù)組形式
/// get獲取當(dāng)前的數(shù)據(jù)
/// set將新來(lái)的數(shù)據(jù)插入到ConcreteAggregate中
public object this[int index]
{
get { return items[index]; }
set { items.Insert(index, value); }
}
}
主函數(shù)調(diào)用:
static void Main(string[] args)
{
ConcreteAggregate a = new ConcreteAggregate();
a[0] = "A";
a[1] = "B";
a[2] = "C";
a[3] = "D";
a[4] = "E";
a[5] = "F";
Iterator i = new ConcreteIterator(a);
object item = i.First();
while (!i.IsDone())
{
Console.WriteLine("{0} buy ticket,please", i.CurrentItem());
i.Next();
}
Iterator id = new ConcreteIteratorDesc(a);
object itemdec = id.First();
while (!id.IsDone())
{
Console.WriteLine("{0} buy ticket,please", id.CurrentItem());
id.Next();
}
Console.Read();
}
.NET的迭代器實(shí)現(xiàn)
迭代器模式在我們現(xiàn)在的使用中其實(shí)沒(méi)有那么麻煩,因?yàn)?NET框架已經(jīng)準(zhǔn)備好了相關(guān)的接口,只需要實(shí)現(xiàn)就好了。
static void Main(string[] args)
{
IList<string> a = new List<string>();
a.Add("A");
a.Add("B");
a.Add("C");
a.Add("D");
a.Add("E");
a.Add("F");
//看見(jiàn)遍歷首先考慮foreach
foreach (string item in a)
{
Console.WriteLine("{0} buy ticket,please", item);
}
//支持在泛型集合上進(jìn)行簡(jiǎn)單迭代。
IEnumerator<string> e = a.GetEnumerator();
while (e.MoveNext())
{
Console.WriteLine("{0} buy ticket,please", e.Current);
}
Console.Read();
}
補(bǔ)充:IEnumerator
備注:文中所有代碼及知識(shí)點(diǎn)均來(lái)自于《大話設(shè)計(jì)模式》,本人屬于邊學(xué)邊看邊敲代碼邊總結(jié)的階段。
以上這篇C#學(xué)習(xí)筆記整理-迭代器模式介紹就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持我們。
上一篇:基于數(shù)據(jù)類(lèi)型轉(zhuǎn)換(裝箱與拆箱)與常量詳解
欄 目:C#教程
下一篇:C# 啟動(dòng) SQL Server 服務(wù)的實(shí)例
本文標(biāo)題:C#學(xué)習(xí)筆記整理-迭代器模式介紹
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5376.html
您可能感興趣的文章
- 01-10C#影院售票系統(tǒng)畢業(yè)設(shè)計(jì)(1)
- 01-10輕松學(xué)習(xí)C#的運(yùn)算符
- 01-10輕松學(xué)習(xí)C#的基礎(chǔ)入門(mén)
- 01-10輕松學(xué)習(xí)C#的裝箱與拆箱
- 01-10輕松學(xué)習(xí)C#的預(yù)定義數(shù)據(jù)類(lèi)型
- 01-10輕松學(xué)習(xí)C#的foreach迭代語(yǔ)句
- 01-10輕松學(xué)習(xí)C#的讀寫(xiě)操作
- 01-10輕松學(xué)習(xí)C#的String類(lèi)
- 01-10輕松學(xué)習(xí)C#的ArrayList類(lèi)
- 01-10輕松學(xué)習(xí)C#的密封類(lèi)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?


