设计模式系列漫谈之五 - 迭代器模式
生活随笔
收集整理的這篇文章主要介紹了
设计模式系列漫谈之五 - 迭代器模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
故事
????小美是小雪最好的朋友。有一天, 小美問小雪:“小雪,怎么你的手機短信這么多啊,是不是談戀愛了?”,小雪笑了一下,說:“還沒呢”,小雪停頓了一下,“可是喜歡我的男孩倒是很多,這些男孩都不錯,眼都快花了”。小美想了想:“跟我說說這些男孩子,我幫你選一個最優秀的”。
??????果然,小美開始給小雪策劃了一個鉆石王老五大調查(遍歷):
??? 1) 年齡小于30大于35者出局;
??? 2) 身高低于180cm者出局;
??? 3) 無房者出局;
??? 4) 無車者出局;
??? 5) 年薪低于100萬者出局。
迭代器模式(ITerator)的解決方案
?????? 迭代器模式的意圖:通過順序遍歷或逆序遍歷等方式訪問集合對象(List)中的各元素子對象(object)。一般來說,一個集合對象擁有兩個職責:一是存儲一組元素子對象;二是遍歷集合中的各元素子對象。從依賴性來講,前者是集合對象的根本屬性;后者是可分離的變化點。
子對象接口如下:
{
????public?interface?IBoy
????{
????????string?Name{get;}
????????int?GetAge();
????????int?GetHeight();
????????bool?HasHouse();
????????bool?HasCar();
????????bool?HasMillion();
????}
}
子對象具體類如下:
namespace?XiaoXue{
????public?class?BoyA?:?IBoy
????{
????????private?string?_Name="張三";
????????public?string?Name
????????{
????????????get{?return?_Name;}
????????}
????????public?int?GetAge()
????????{
????????????return?25;
????????}
????????public?int?GetHeight()
????????{
????????????return?187;
????????}
????????public?bool?HasHouse()
????????{
????????????return?true;
????????}
????????public?bool?HasCar()
????????{
????????????return?false;
????????}
????????public?bool?HasMillion()
????????{
????????????return?false;
????????}
????}
???public?class?BoyB?:?IBoy
????{
????????private?string?_Name="李四";
????????public?string?Name
????????{
????????????get{?return?_Name;}
????????}
????????public?int?GetAge()
????????{
????????????return?32;
????????}
????????public?int?GetHeight()
????????{
????????????return?170;
????????}
????????public?bool?HasHouse()
????????{
????????????return?true;
????????}
????????public?bool?HasCar()
????????{
????????????return?true;
????????}
????????public?bool?HasMillion()
????????{
????????????return?true;
????????}
????}
}
迭代器接口如下:
namespace?XiaoXue{
???????public?interface?IListIterator
???????{
???????????void?First();
???????????void?Last();
???????????bool?MovePrevous();
???????????bool?MoveNext();
???????????object?Current{get;}
???????}
}
迭代器如下:
namespace?XiaoXue{
???????public?class?BoyList:IListIterator
???????{
???????????private?List<IBoy>?boys?=?null;//子對象列表
???????????private?int?index=-1;
???????????public?BoyList()
???????????{
???????????????boys?=?new?List<IBoy>();
???????????}
???????????//添加子對象
???????????public?void?AddBoy(IBoy?boy)
???????????{
???????????????boys.Add(boy);
???????????}
???????????public?void?RemoveBoy(IBoy?boy)
???????????{
???????????????boys.Remove(boy);
???????????????index--;
???????????}
???????????public?void?First()
???????????{
???????????????index=0;
???????????}
???????????public?void?Last()
???????????{
???????????????index=boys.Count;
???????????}
???????????public?bool?MovePrevious()
???????????{
???????????????index--;
???????????????return?index>-1;
???????????}
???????????public?bool?MoveNext()
???????????{
???????????????index++;
???????????????return?index<boys.Count;
???????????}
???????????public?object?Current
???????????{
???????????????get?{return?this.boys[index];}
???????????}
????}
}
調用代碼如下:
IListIterator?iterator=new?BoyList();iterator.AddBoy(new?BoyA());
iterator.AddBoy(new?BoyB());
while?(iterator.MoveNext())
{
???????IBoy?boy=(IBoy)iterator.Current
???????if?(boy.GetAge()<30?||?boy.GetAge()>35)
???????{
???????????iterator.RemoveBoy(boy);
???????}
???????else?if?(boy.GetHeight()<180)
???????{
???????????iterator.RemoveBoy(boy);
???????}
???????else?if?(!boy.HasHouse())
???????{
???????????iterator.RemoveBoy(boy);
???????}
???????else?if?(!boy.HasCar())
???????{
???????????iterator.RemoveBoy(boy);
???????}
???????else?if?(!boy.HasMillion())
???????{
???????????iterator.RemoveBoy(boy);
???????}
???????else
???????{
??????????????Console.WriteLine(boy.Name);
???????}
}
哈哈, 好象沒有一個符合條件的,俗話說:“什么樣的馬配什么樣的鞍”。這樣的王老五只有到月球上去找吧!
轉載于:https://www.cnblogs.com/ruochen/archive/2007/12/20/1006772.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的设计模式系列漫谈之五 - 迭代器模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深入Managed DirectX9(二
- 下一篇: 【转】20个让Web Developer