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

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

設計模式簡介(一)——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());
}
}
}

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





主站蜘蛛池模板: 在线观看亚洲免费 | 亚洲激情欧美 | 天美传媒一区二区三区 | 婷婷九月丁香 | 日韩精品成人免费观看 | 三级黄色免费 | 青青青青青在线视频播放 | 天堂va| 午夜欧美成人久久久久久 | 小说区图片区综合久久亚洲 | 色综合天天综合网国产国产人 | 日本综合色 | 伊人久热这里只有精品视频99 | 日本免费不卡视频 | 欧美在线一级视频 | 日韩精品视频免费在线观看 | 青青草色 | 午夜视频在线观看完整高清在线 | 亚洲成人一级 | 日韩欧美高清在线观看 | 欧美伊人久久大香线蕉在观 | 午夜精品福利在线 | 日韩高清在线不卡 | 一区二区三区在线免费视频 | 日本中文字幕第一页 | 中文字幕在线免费观看 | 亚洲 丝袜 制服 欧美 另类 | 四虎4hu影库免费永久国产 | 青青爽国产手机在线观看免费 | 亚洲欧美日韩国产 | 欧美在线黄色 | 亚洲第一页综合 | 亚洲国产精品线播放 | 午夜在线观看视频在线播放版 | 欧美中文字幕在线 | 亚洲第一视频网站 | 日日天天 | 伊人精品在线观看 | 日本一区二区高清免费不卡 | 日韩精品一区二区三区 在线观看 | 在线亚洲精品中文字幕美乳 |