22設計模式迭代器模式(Iterator).ppt_第1頁
22設計模式迭代器模式(Iterator).ppt_第2頁
22設計模式迭代器模式(Iterator).ppt_第3頁
22設計模式迭代器模式(Iterator).ppt_第4頁
22設計模式迭代器模式(Iterator).ppt_第5頁
已閱讀5頁,還剩28頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、設計模式(Design Pattern),張凱 副教授 計算機學院 軟件工程系,問題(Problem),遍歷二維數(shù)組,問題(Problem),遍歷三維數(shù)組,問題(Problem),每個人都要買票,問題(Problem),static void Main(string args) ArrayList a = new ArrayList() ; a.Add(大鳥); a.Add(小菜); a.Add(行李); a.Add(老外); a.Add(公交內部員工); a.Add(“小朋友); foreach (string item in a) Console.WriteLine(0 請買車票!, it

2、em); Console.Read(); ,主要內容,迭代器模式(Iterator),模式動機 模式名稱:迭代器模式(Iterator) 一個聚合對象,如一個列表(List)或者一個集合(Set),應該提供一種方法來讓別人可以訪問它的元素,而又不需要暴露它的內部結構。 針對不同的需要,可能還要以不同的方式遍歷整個聚合對象,但是我們并不希望在聚合對象的抽象層接口中充斥著各種不同遍歷的操作。 怎樣遍歷一個聚合對象,又不需要了解聚合對象的內部結構,還能夠提供多種不同的遍歷方式,這就是迭代器模式所要解決的問題。,迭代器模式(Iterator),模式動機 在迭代器模式中,提供一個外部的迭代器來對聚合對象

3、進行訪問和遍歷,迭代器定義了一個訪問該聚合元素的接口,并且可以跟蹤當前遍歷的元素,了解哪些元素已經遍歷過而哪些沒有。 有了迭代器模式,我們會發(fā)現(xiàn)對一個復雜的聚合對象的操作會變得如此簡單。,迭代器模式(Iterator),模式定義 迭代器模式(Iterator Pattern) :提供一種方法來訪問聚合對象,而不用暴露這個對象的內部表示,其別名為游標(Cursor)。迭代器模式是一種對象行為型模式。,迭代器模式(Iterator),模式結構,關聯(lián)關系,依賴關系,迭代器模式(Iterator),參與者 Iterator: 抽象迭代器 ConcreteIterator: 具體迭代器 Aggregat

4、e: 抽象聚合類 ConcreteAggregate: 具體聚合類,迭代器模式(Iterator),索引器,索引器允許類和結構的實例用下標進行訪問,這種方式類似于訪問數(shù)組、集合的元素,但不單單是通過下標,例如通過對象的各屬性值也是可以的。索引器寫在類或者結構中,其格式類似于:,publicobjectthisintindex get returnobjectsint; set objectsint = value; ,索引器是特殊的屬性,可以有get、set訪問器,可以重載。object是返回的對象類型,this代表類或結構自己,int index表示索引類型。這與索引器的調用格式:對象索引,

5、相對應。,迭代器模式(Iterator),publicclassCar / / 品牌 / publicstringBrand get; set; / / 顏色 / publicstringColor get; set; / / 覆蓋object的ToString()方法 / / publicoverridestringToString() returnstring.Format(A 0 1., this.Color, this.Brand); ,迭代器模式(Iterator),publicclassCars publicIList List get; set; / / 用下標訪問的索引器 /

6、/ 下標 / 汽車 publicCar thisintindex get returnthis.Listindex; / / 用品牌訪問的索引器 / / 品牌 / 汽車 publicCar thisstringbrand get for(inti = 0; i List.Count; i+) if(brand.Equals(this.Listi.Brand) returnthis.Listi; returnnull; ,迭代器模式(Iterator),publicstaticvoidMain(string args) Cars cars = newCars List = newList new

7、Car Brand = BMW, Color = Black, / 黑色寶馬 newCar Brand = Benz, Color = Red, / 紅色奔馳 newCar Brand = Porsche, Color = Yellow / 黃色保時捷 ; Console.Title = 索引器; Console.WriteLine(cars0.ToString(); Console.WriteLine(carsPorsche.ToString(); Console.ReadLine(); ,迭代器模式(Iterator),模式結構,關聯(lián)關系,依賴關系,迭代器模式(Iterator),abst

8、ract class Iterator public abstract object First(); public abstract object Next(); public abstract bool IsDone(); public abstract object CurrentItem(); ,abstract class Aggregate public abstract Iterator CreateIterator(); ,迭代器模式(Iterator),class ConcreteAggregate : Aggregate private IList items = new

9、List(); public override Iterator CreateIterator() return new ConcreteIterator(this); public int Count get return items.Count; public object thisint index get return itemsindex; set items.Insert(index, value); ,迭代器模式(Iterator),class ConcreteIterator : Iterator private ConcreteAggregate aggregate; pri

10、vate int current = 0; public ConcreteIterator(ConcreteAggregate aggregate) this.aggregate = aggregate; public override object First() return aggregate0; public override object Next() object ret = null; current+; if (current = aggregate.Count ? true : false; ,迭代器模式(Iterator),探究Foreach,迭代器模式(Iterator)

11、,static void Main(string args) ArrayList a = new ArrayList(); a.Add(大鳥); a.Add(小菜); a.Add(行李); a.Add(老外); a.Add(公交內部員工); a.Add(小朋友); foreach (string item in a) Console.WriteLine(0 請買車票!, item); Console.Read(); ,迭代器模式(Iterator),迭代器模式(Iterator),迭代器模式(Iterator),迭代器模式(Iterator),ArrayList a = new ArrayLi

12、st(); foreach (string item in a) Console.WriteLine(0 請買車票!, item); IEnumerator enumerator = a.GetEnumerator(); while(enumerator.MoveNext() string item = (string)enumerator.Current; Console.WriteLine(0 請買車票!, item); ,迭代器模式(Iterator),public virtual IEnumerator GetEnumerator() return new ArrayListEnume

13、ratorSimple(this);,追本溯源,ArrayList類型間接地實現(xiàn)了IEnumerable接口。在ArrayList中,IEnumerable接口的GetEnumerator()方法實現(xiàn)代碼如下:, 開放了.Net Framework 4.5源代碼,迭代器模式(Iterator),迭代器模式(Iterator),模式結構,迭代器模式(Iterator),模式結構,IEnumerator,ArrayList,ArrayListEnumeratorSimple,IList,public virtual IEnumerator GetEnumerator() return new ArrayListEnumeratorSimple(this);,迭代器模式(Iterator),迭代器模式的優(yōu)點如下 它支持以不同的方式遍歷一個聚合對象。 迭代器簡化了聚合類。 在同一個聚合上可以有多個遍歷。 在迭代器模式中,增加新的聚合類和迭代器類都很方便,無須

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論