java常用设计模式之迭代器模式
程序员文章站
2022-05-04 13:17:39
...
------------------------------ 迭代器模式(Iterator) -------------------------------
(1)
基本概念:
迭代器模式属于行为型模式,其意图是提供一种方法顺序访问一个聚合对象中得各个元素,而又不需要暴露该对象的
内部表示。
至少可以历遍first,next,previous,last,isOver,或是历遍选择符合某种条件的子元素.
(2)
结构:
由一个接口与一个实现类组成.
接口:
主要是定义各历遍的方法.
实现类:
需要一个计算点private int current=0 ; 以及一个容器Vector,来存在原来的进行历遍的一团东西;再对接口方法进
至少可以历遍first,next,previous,last,isOver,或是历遍选择符合某种条件的子元素.
(2)
结构:
由一个接口与一个实现类组成.
接口:
主要是定义各历遍的方法.
实现类:
需要一个计算点private int current=0 ; 以及一个容器Vector,来存在原来的进行历遍的一团东西;再对接口方法进
行实现.
(3)
实例:
Iterator接口:
package iterator;
public interface Iterator{
/*
Item:即是集合中的各对象的类型.若为String,即把所有的ITEM改为String,若为其它自定义的类,则改为各自定义的类
实例:
Iterator接口:
package iterator;
public interface Iterator{
/*
Item:即是集合中的各对象的类型.若为String,即把所有的ITEM改为String,若为其它自定义的类,则改为各自定义的类
的接口,或类. --->important.
*/
public Item first();
public Item next();
public boolean isDone();
public Item currentItem();
}
*/
public Item first();
public Item next();
public boolean isDone();
public Item currentItem();
}
Controller类实现了Iterator接口。
package iterator;
import java.util.Vector;
public class Controller implements Iterator{
private int current =0;
Vector channel;
public Controller(Vector v){
channel = v;
}
public Item first(){
current = 0;
return (Item)channel.get(current);
}
public Item next(){
current ++;
return (Item)channel.get(current);
}
public Item currentItem(){
return (Item)channel.get(current);
}
public boolean isDone(){
return current>= channel.size()-1;
}
}
package iterator;
import java.util.Vector;
public class Controller implements Iterator{
private int current =0;
Vector channel;
public Controller(Vector v){
channel = v;
}
public Item first(){
current = 0;
return (Item)channel.get(current);
}
public Item next(){
current ++;
return (Item)channel.get(current);
}
public Item currentItem(){
return (Item)channel.get(current);
}
public boolean isDone(){
return current>= channel.size()-1;
}
}
Television接口:
package iterator;
import java.util.Vector;
public interface Television{
public Iterator createIterator();
}
HaierTV类实现了Television接口。
package iterator;
import java.util.Vector;
public class HaierTV implements Television{ ---对象
private Vector channel;
public HaierTV(){
channel = new Vector();
channel.addElement(new Item("channel 1")); --各元素,用VECTOR存放
channel.addElement(new Item("channel 2"));
channel.addElement(new Item("channel 3"));
channel.addElement(new Item("channel 4"));
channel.addElement(new Item("channel 5"));
channel.addElement(new Item("channel 6"));
channel.addElement(new Item("channel 7"));
}
public Iterator createIterator(){
return new Controller(channel); --把这个VECTOR放到迭代器中构造方法中去
}
}
Client客户端:
package iterator;
public class Client{
public static void main(String[] args){
Television tv = new HaierTV();
Iterator it =tv.createIterator();
System.out.println(it.first().getName());
while(!it.isDone()){
System.out.println(it.next().getName());
}
}
}
Item类的接口:
package iterator;
public class Item{
private String name;
public Item(String aName){
name = aName;
}
public String getName(){
return name;
}
}
package iterator;
import java.util.Vector;
public interface Television{
public Iterator createIterator();
}
HaierTV类实现了Television接口。
package iterator;
import java.util.Vector;
public class HaierTV implements Television{ ---对象
private Vector channel;
public HaierTV(){
channel = new Vector();
channel.addElement(new Item("channel 1")); --各元素,用VECTOR存放
channel.addElement(new Item("channel 2"));
channel.addElement(new Item("channel 3"));
channel.addElement(new Item("channel 4"));
channel.addElement(new Item("channel 5"));
channel.addElement(new Item("channel 6"));
channel.addElement(new Item("channel 7"));
}
public Iterator createIterator(){
return new Controller(channel); --把这个VECTOR放到迭代器中构造方法中去
}
}
Client客户端:
package iterator;
public class Client{
public static void main(String[] args){
Television tv = new HaierTV();
Iterator it =tv.createIterator();
System.out.println(it.first().getName());
while(!it.isDone()){
System.out.println(it.next().getName());
}
}
}
Item类的接口:
package iterator;
public class Item{
private String name;
public Item(String aName){
name = aName;
}
public String getName(){
return name;
}
}
下一篇: selenium爬取百度图片