惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
爱范儿
爱范儿
月光博客
月光博客
The GitHub Blog
The GitHub Blog
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog
T
Tailwind CSS Blog
美团技术团队
D
Docker
V
Visual Studio Blog
Martin Fowler
Martin Fowler
博客园 - 聂微东
The Cloudflare Blog

博客园 - AlanPaoPao

23)Visitor 22)Template 21)Strategy 20)State 19)Observer 18)Memento 17)Mediator 15)Interpreter 14)Command 13)Chain Of Responsibility 12)Proxy 11)Flyweight 10)Facade 09)Decorator 08)Composite 07)Bridge 06)Adapter 05)Prototype 04)Factory Method
16)Iterator
AlanPaoPao · 2007-09-21 · via 博客园 - AlanPaoPao

    迭代器模式的目的是: 提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示
    实例代码:

class Item
{
  
string name;
  
public Item(string name)
  
{
    
this.name = name;
  }

  
public string Name
  
{
    
get return name; }
  }

}

interface IAbstractIterator
{
  Item First();
  Item Next();
  
bool IsDone get; }
  Item CurrentItem 
get; }
}

class Iterator : IAbstractIterator
{
  
private Collection collection;
  
private int current = 0;
  
private int step = 1;
  
public Iterator(Collection collection)
  
{
    
this.collection = collection;
  }

  
public Item First()
  
{
    current 
= 0;
    
return collection[current] as Item;
  }

  
public Item Next()
  
{
    current 
+= step;
    
if (!IsDone)
      
return collection[current] as Item;
    
else
      
return null;
  }

  
public int Step
  
{
    
get return step; }
    
set { step = value; }
  }

  
public Item CurrentItem
  
{
    
get
    
{
      
return collection[current] as Item;
    }

  }

  
public bool IsDone
  
{
    
get
    
{
      
return current >= collection.Count ? true : false;
    }

  }

}

interface IAbstractCollection
{
  Iterator CreateIterator();
}

class Collection : IAbstractCollection
{
  
private ArrayList items = new ArrayList();
  
public Iterator CreateIterator()
  
{
    
return new Iterator(this);
  }

  
public int Count
  
{
    
get return items.Count; }
  }

  
public object this[int index]
  
{
    
get return items[index]; }
    
set { items.Add(value); }
  }

}

class MainApp
{
  
static void Main()
  
{
    Collection collection 
= new Collection();
    collection[
0= new Item("Item 0");
    collection[
1= new Item("Item 1");
    collection[
2= new Item("Item 2");
    collection[
3= new Item("Item 3");
    collection[
4= new Item("Item 4");
    collection[
5= new Item("Item 5");
    collection[
6= new Item("Item 6");
    collection[
7= new Item("Item 7");
    collection[
8= new Item("Item 8");
    Iterator iterator 
= new Iterator(collection);
    iterator.Step 
= 2;
    Console.WriteLine(
"Iterating over collection:");
    
for (Item item = iterator.First();!iterator.IsDone; item = iterator.Next())
    
{
      Console.WriteLine(item.Name);
    }

    Console.Read();
  }

}