六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

設計模式簡介(一)——Iterator

[摘要][目的] 提供一種方法順序訪問一個聚合對象中各個元素, 而又不需暴露該對象的內部表示。 [何時使用?] 訪問一個聚合對象的內容而無需暴露它的內部表示。支持對聚合對象的多種遍歷。為遍歷不同的聚合結構提供一個統一的接口(即, 支持多態迭代)。 [如何使用?] Java風格的示例程序: //抽象的集合 ...

[目的]
提供一種方法順序訪問一個聚合對象中各個元素, 而又不需暴露該對象的內部表示。

 [何時使用?]
訪問一個聚合對象的內容而無需暴露它的內部表示。支持對聚合對象的多種遍歷。為遍歷不同的聚合結構提供一個統一的接口(即, 支持多態迭代)。

[如何使用?]
Java風格的示例程序:

//抽象的集合
public interface Aggregate
{

public abstract Iterator iterator();
}

//抽象的Iterator
public interface Iterator
{

public abstract boolean hasNext();
public abstract Object next();
}

//具體的集合
public class ConcreteAggregate implements Aggregate
{

private Object[] collection;
private int last = 0;
public ConcreteAggregate()
{

collection = new Object[3];
}

public Object getItemAt(int index)
{

return collection[index];
}

public void appendItem(Object item)
{

this.collection[last] = item;
last++;
}

public int getLength()
{

return last;
}

public Iterator iterator()
{

//產生適合自己的Iterator
return new ConcreteIterator(this);
}
}

//具體的Iterator
public class ConcreteIterator implements Iterator
{

private ConcreteAggregate namecollection; //由它決定遍歷的集合類型
private int index;
public ConcreteIterator(ConcreteAggregate collection)
{

this.namecollection = collection;
this.index = 0;
}

public boolean hasNext()
{

if (index < namecollection.getLength())
{

return true;
}
else
{

return false;
}
}
public Object next()
{

//通過統一的getItemAt()接口實現對不同類型集合的遍歷
Object item = namecollection.getItemAt(index);
index++;
return item;
}
}

public class IteratorExample
{
public static void main(String[] args)
{
ConcreteAggregate collection = new ConcreteAggregate();
collection.appendItem(new Person("Davis"));
collection.appendItem(new Person("Frank"));
collection.appendItem(new Person("Jeny"));

//it的具體類型依賴于collection的類型而使用者并不需要關心這里面的區別

Iterator it = collection.iterator(); //返回適合collection的iterator
while (it.hasNext())
{
Person person = (Person)it.next();
System.out.println("" + person.getName());
}
}
}

這樣遍歷代碼保持了統一的風格,從而使使用者不必關心遍歷的對象到底是什么類型。





主站蜘蛛池模板: 日本大片在线 | 亚洲精品视频在线观看你懂的 | 欧美一区二区三区四区在线观看 | 欧美整片第一页 | 在线高清h视频在线观看 | 日韩 欧美 亚洲 中文字幕 | 欧美在线观看视频网站 | 日本一二区在线观看 | 婷婷性 | 午夜在线观看免费高清在线播放 | 欧美一区二区三区黄色 | 日本韩国中文字幕 | 亚欧国产 | 亚洲mm8成为人影院 亚洲h在线观看 | 亚洲热播| 桃花视频全免费观看韩国 | 欧美中日韩在线 | 欧美一级xxxx俄罗斯一级 | 亚洲国产欧美在线人成精品一区二区 | 亚洲欧美精品在线 | 亚洲美女色在线欧洲美女 | 深夜毛片| 庆余年第二部在线观看免费完整版 | 亚洲一级片免费 | 午夜影院黄色片 | 四虎成人免费大片在线 | 天天伊人网 | 视频在线免费高清 | 在线 色 | 日本一区二区三区中文字幕视频 | 五月综合色 | 五月婷婷综合激情 | 污污小视频在线观看 | 亚洲精品91在线 | 色噜噜狠狠狠狠色综合久不 | 日韩精品中文字幕久久 | 日韩中文网 | 色悠久久久久综合网伊人男男 | 欧美综合精品一区二区三区 | 亚洲 自拍 中文字幕 在线 | 日韩一区国产一级 |