Iterator,迭代器模式:提供一个方法顺序访问一个聚合对象的各个元素,而又不需要暴露该对象的内部表示。
package com.cbf4life;
public interface IProject {
//从老板这里看到的就是项目信息
public String getProjectInfo();
}
package com.cbf4life;
public class Project implements IProject {
//项目名称
private String name = "";
//项目成员数量
private int num = 0;
//项目费用
private int cost = 0;
public Project(String name,int num,int cost){
this.name = name;
this.num = num;
this.cost=cost;
}
public String getProjectInfo() {
String info = "";
info = info+ "项目名称是:" + this.name;
info = info + "\t项目人数: "+ this.num;
info = info+ "\t 项目费用:"+ this.cost;
return info;
}
}
package com.cbf4life;
import java.util.ArrayList;
/**
* 老板来看项目信息了
*/
public class Boss {
public static void main(String[] args) {
ArrayList<IProject> projectList = new ArrayList<IProject>();
projectList.add(new Project("星球大战项目",10,100000));
projectList.add(new Project("扭转时空项目",100,10000000));
projectList.add(new Project("超人改造项目",10000,1000000000));
for(int i=4;i<104;i++){
projectList.add(new Project("第"+i+"个项目",i*5,i*1000000));
}for(IProject project:projectList){
System.out.println(project.getProjectInfo());
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.cbf4life.pattern;
public interface IProject {
//增加项目
public void add(String name,int num,int cost);
//从老板这里看到的就是项目信息
public String getProjectInfo();
//获得一个可以被遍历的对象
public IProjectIterator iterator();
} package com.cbf4life.pattern;
import java.util.ArrayList;
@SuppressWarnings("all")
public class Project implements IProject {
private ArrayList<IProject> projectList = new ArrayList<IProject>();
private String name = "";
private int num = 0;
private int cost = 0;
public Project(){
}
private Project(String name,int num,int cost){
this.name = name;
this.num = num;
this.cost=cost;
}
public void add(String name,int num,int cost){
this.projectList.add(new Project(name,num,cost));
}
public String getProjectInfo() {
String info = "";
info = info+ "项目名称是:" + this.name;
info = info + "\t项目人数: "+ this.num;
info = info+ "\t 项目费用:"+ this.cost;
return info;
}
public IProjectIterator iterator(){
return new ProjectIterator(this.projectList);
}
}
package com.cbf4life.pattern;
import java.util.Iterator;
@SuppressWarnings("all")
public interface IProjectIterator extends Iterator {
}
package com.cbf4life.pattern;
import java.util.ArrayList;
public class ProjectIterator implements IProjectIterator {
private ArrayList<IProject> projectList = new ArrayList<IProject>();
private int currentItem = 0;
//构造函数传入projectList
public ProjectIterator(ArrayList<IProject> projectList){
this.projectList = projectList;
}
//判断是否还有元素,必须实现
public boolean hasNext() {
//定义一个返回值
boolean b = true;
if(this.currentItem>=projectList.size() ||
this.projectList.get(this.currentItem) == null){
b =false;
}
return b;
}
//取得下一个值
public IProject next() {
return (IProject)this.projectList.get(this.currentItem++);
}
//删除一个对象
public void remove() {
//暂时没有使用到
}
}
//都写完毕了,然后看看我们的Boss 类有多少改动:
package com.cbf4life.pattern;
public class Boss {
public static void main(String[] args) {
IProject project = new Project();
project.add("星球大战项目ddddd",10,100000);
project.add("扭转时空项目",100,10000000);
project.add("超人改造项目",10000,1000000000);
//这边100个项目
for(int i=4;i<104;i++){
project.add("第"+i+"个项目",i*5,i*1000000);
}
IProjectIterator projectIterator = project.iterator();
while(projectIterator.hasNext()){
IProject p = (IProject)projectIterator.next();
System.out.println(p.getProjectInfo());
}
}
}