前言:【模式总览】——————————by xingoo

  模式意图

  提供一个方法按顺序遍历一个集合内的元素,而又不需要暴露该对象的内部表示。

  应用场景

  1 访问一个聚合的对象,而不需要暴露对象的内部表示

  2 支持对聚合对象的多种遍历

  3 对遍历不同的对象,提供统一的接口。

  模式结构

  Iterator 定义访问的接口

/**
* 抽象的迭代,有判断结束和下一个,获取当前元素等函数
* @author xingoo
*
*/
interface Iterator{
void first();
void next();
boolean isDone();
Object currentItem();
}

  ConcreteIterator 具体的迭代器,跟踪聚合内的元素

/**
* 具体的迭代类
* @author xingoo
*
*/
class ConcreteIterator implements Iterator{
private ConreteAggregate agg;
private int index = ;
private int size = ; public ConcreteIterator(ConreteAggregate agg) {
this.agg = agg;
size = agg.size();
index = ;
} public void first() {
index = ;
} public void next() {
if(index < size){
index++;
}
} public boolean isDone() {
return (index >= size);
} public Object currentItem() {
return agg.getElement(index);
} }

  Aggregate 提供聚合的接口

/**
* 聚合的类
* @author xingoo
*
*/
abstract class Aggregate{
public Iterator createIterator(){
return null;
}
}

  ConcreteAggregate 具体的聚合

/**
* 具体的聚合对象,拥有大小,创建迭代子等函数
* @author xingoo
*
*/
class ConreteAggregate extends Aggregate{
private Object[] obj = {"test1","test2","test3","test4"};
public Iterator createIterator(){
return new ConcreteIterator(this);
}
public Object getElement(int index){
if(index < obj.length){
return obj[index];
}else{
return null;
}
}
public int size(){
return obj.length;
}
}

  全部代码

 package com.xingoo.Iterator;
/**
* 聚合的类
* @author xingoo
*
*/
abstract class Aggregate{
public Iterator createIterator(){
return null;
}
}
/**
* 抽象的迭代,有判断结束和下一个,获取当前元素等函数
* @author xingoo
*
*/
interface Iterator{
void first();
void next();
boolean isDone();
Object currentItem();
}
/**
* 具体的聚合对象,拥有大小,创建迭代子等函数
* @author xingoo
*
*/
class ConreteAggregate extends Aggregate{
private Object[] obj = {"test1","test2","test3","test4"};
public Iterator createIterator(){
return new ConcreteIterator(this);
}
public Object getElement(int index){
if(index < obj.length){
return obj[index];
}else{
return null;
}
}
public int size(){
return obj.length;
}
}
/**
* 具体的迭代类
* @author xingoo
*
*/
class ConcreteIterator implements Iterator{
private ConreteAggregate agg;
private int index = ;
private int size = ; public ConcreteIterator(ConreteAggregate agg) {
this.agg = agg;
size = agg.size();
index = ;
} public void first() {
index = ;
} public void next() {
if(index < size){
index++;
}
} public boolean isDone() {
return (index >= size);
} public Object currentItem() {
return agg.getElement(index);
} }
/**
* 客户端 使用方法
* @author xingoo
*
*/
public class Client {
private Iterator it;
private Aggregate agg = new ConreteAggregate();
public void operation(){
it = agg.createIterator();
while(!it.isDone()){
System.out.println(it.currentItem().toString());
it.next();
}
}
public static void main(String[] args) {
Client client = new Client();
client.operation();
}
}

  运行结果

test1
test2
test3
test4

【设计模式】—— 迭代模式Iterator的更多相关文章

  1. 设计模式 - 迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释

    迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考迭代器模式(ite ...

  2. javascript设计模式-迭代器模式(Iterator)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 19迭代模式Iterator

    一.什么是迭代模式 Iterator模式也叫迭代模式,是行为模式之 一,它把对容器中包含的内部对象的访问委让给 外部类,使用Iterator(遍历)按顺序进行遍历 访问的设计模式. 二.不使用迭代模式 ...

  4. [工作中的设计模式]迭代子模式Iterator

    一.模式解析 迭代子模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象 1.迭代子模式一般用于对集合框架的访问,常用的集合框架为lis ...

  5. 设计模式 - 迭代器模式(iterator pattern) 具体解释

    迭代器模式(iterator pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 迭代器模式(iterator pattern) : 提供一 ...

  6. 24种设计模式--迭代模式【Iterator Pattern】

    周五下午,我正在看技术网站,第六感官发觉有人在身后,扭头一看,我 C,老大站在背后,赶忙站起来,“王经理,你找我?” 我说. “哦,在看技术呀.有个事情找你谈一下,你到我办公室来一下.” 老大说. 到 ...

  7. 设计模式 -- 迭代器模式(Iterator)

    --------------------------------------------------------------------- 1.场景问题 考虑这样一个问题: 9个学生对象分别通过数组存 ...

  8. PHP设计模式——迭代模式

    声明:这一系列的博客引用<大话设计模式>.程洁作者. 迭代器模式:迭代器模式是遍历集合的成熟模式.迭代器模式的关键是将遍历集合的任务交给一个叫做迭代器的对象,它的工作时遍历并选择序列中的对 ...

  9. C#设计模式——迭代器模式(Iterator Pattern)

    一.概述在软件开发过程中,我们可能会希望在不暴露一个集合对象内部结构的同时,可以让外部代码透明地访问其中包含的元素.迭代器模式可以解决这一问题.二.迭代器模式迭代器模式提供一种方法顺序访问一个集合对象 ...

随机推荐

  1. Android处理ListView中的Item中的Button按钮不能点击的问题

    问题描述:ListView列表中的Button按钮按钮不能点击 解决办法:在ListView中的Item项的布局文件中加上:android:descendantFocusability="b ...

  2. 四,ESP8266 TCP服务器(基于Lua脚本语言)

    我要赶时间赶紧写完所有的内容....朋友的东西答应的还没做完呢!!!!!!!没想到又来了新的事情,,....... 配置模块作为TCP服务器然后呢咱们连接服务器发指令控制LED亮灭 控制的指令呢咱就配 ...

  3. 关于开发React Native的注意事项

    今天在写一个简单的RN的Demo时,一连出现了好几个错误,最后幸亏得以解决,在这里把我踩过的坑以及解决办法分享出来: 1.运行出现错误:Could not connect to development ...

  4. 20155310《网络对抗》Exp2 后门原理与实践

    20155310<网络对抗>Exp2 后门原理与实践 基础问题回答 1.例举你能想到的一个后门进入到你系统中的可能方式? 在网上下载软件的时候,后门很有可能被捆绑在下载的软件当中: 浏览网 ...

  5. VS编程,编辑WPF过程中,点击设计器中界面某一控件,在XAML中高亮突出显示相应的控件代码的设置方法。

    原文:VS编程,编辑WPF过程中,点击设计器中界面某一控件,在XAML中高亮突出显示相应的控件代码的设置方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net ...

  6. WinRT IO相关整理

    虽然一般UWP开发还是依赖.Net for UWP,但有时还是需要调用WinRT API.特别是在IO部分,WinRT有着和.Net似曾相识但又不尽相同的接口.在此对经常用到的一些地方进行一下整理. ...

  7. 微信小程序之可滚动视图容器组件 scroll-view

    1. 纵向滚动 scroll-y 当 设置为scroll-y 时, 需要将其高度设为固定值 如果整个页面,即最外层标签为scroll-view,需要并将其高度设为100%,也需要将page设为100% ...

  8. JQuery快速入门-事件与效果

    一.事件 事件绑定的方法有两种: 绑定到元素 查找元素后绑定事件 方法1:绑定到元素 <body> <p onclick='func1()'>点击我</p> < ...

  9. stl源码剖析 详细学习笔记deque(1)

    //--------------------------15/3/12---------------------------- deque { deque没有容量(capacity)观念,是动态分段的 ...

  10. Outlook2013修改数据文件默认存放目录

    转载 当使用outlook 2013新建Email账户的时候,其数据文件(.ost文件)总是被保存在C盘默认目录“C:\Users\用户名\AppData\Local\Microsoft\Outloo ...