【设计模式】—— 迭代模式Iterator
前言:【模式总览】——————————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的更多相关文章
- 设计模式 - 迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释
迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考迭代器模式(ite ...
- javascript设计模式-迭代器模式(Iterator)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 19迭代模式Iterator
一.什么是迭代模式 Iterator模式也叫迭代模式,是行为模式之 一,它把对容器中包含的内部对象的访问委让给 外部类,使用Iterator(遍历)按顺序进行遍历 访问的设计模式. 二.不使用迭代模式 ...
- [工作中的设计模式]迭代子模式Iterator
一.模式解析 迭代子模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象 1.迭代子模式一般用于对集合框架的访问,常用的集合框架为lis ...
- 设计模式 - 迭代器模式(iterator pattern) 具体解释
迭代器模式(iterator pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 迭代器模式(iterator pattern) : 提供一 ...
- 24种设计模式--迭代模式【Iterator Pattern】
周五下午,我正在看技术网站,第六感官发觉有人在身后,扭头一看,我 C,老大站在背后,赶忙站起来,“王经理,你找我?” 我说. “哦,在看技术呀.有个事情找你谈一下,你到我办公室来一下.” 老大说. 到 ...
- 设计模式 -- 迭代器模式(Iterator)
--------------------------------------------------------------------- 1.场景问题 考虑这样一个问题: 9个学生对象分别通过数组存 ...
- PHP设计模式——迭代模式
声明:这一系列的博客引用<大话设计模式>.程洁作者. 迭代器模式:迭代器模式是遍历集合的成熟模式.迭代器模式的关键是将遍历集合的任务交给一个叫做迭代器的对象,它的工作时遍历并选择序列中的对 ...
- C#设计模式——迭代器模式(Iterator Pattern)
一.概述在软件开发过程中,我们可能会希望在不暴露一个集合对象内部结构的同时,可以让外部代码透明地访问其中包含的元素.迭代器模式可以解决这一问题.二.迭代器模式迭代器模式提供一种方法顺序访问一个集合对象 ...
随机推荐
- Vue表单绑定(单选按钮,选择框(单选时,多选时,用 v-for 渲染的动态选项)
<!DOCTYPE html><html> <head> <meta charset="utf-8"> ...
- 复习整理2:juit
@FixMethodOrder(MethodSorters.NAME_ASCENDING)测试回环 https://blog.csdn.net/u014294166/article/details/5 ...
- Python中浅拷贝和深拷贝的区别总结与理解
单层浅拷贝 import copy a = 1 # 不可变数据类型 copy_a = copy.copy(a) print(id(a),id(copy_a)) # 内存地址相同 a = [1,2] # ...
- 异步编程之asyncio简单介绍
引言: python由于GIL(全局锁)的存在,不能发挥多核的优势,其性能一直饱受诟病.然而在IO密集型的网络编程里,异步处理比同步处理能提升成百上千倍的效率,弥补了python性能方面的短板. as ...
- Excel读取Word Table元素
Option Explicit Sub Mian() Application.ScreenUpdating = False Application.DisplayAlerts = False Appl ...
- 按键精灵对APP自动化测试(下)
上一篇介绍了安卓app上使用按键精灵的实践,这里再来说说苹果上的app. 由于iOS相关工具对操作系统的限制,目前在iOS10.0.2系统上应用成功. 二. 苹果手机按键精灵APP录制 适 ...
- Jenkins分布式构建
Jenkins分布式构建 有时,如果有一个实例,它是一个更大,更重的项目,需要定期编译生成在许多计算机上.并运行所有这些构建了中央台机器上可能不是最好的选择.在这种情况下,人们可以配置其他Jenkin ...
- python 游戏(船只寻宝)
1. 游戏思路和流程图 实现功能:船只在可以在大海上移动打捞宝藏,船只可以扫描1格范围内的宝藏(后续难度,可以调整扫描范围,可以调整前进的格数) 游戏流程图 2. 使用模块和游戏提示 import r ...
- 基于tensorflow使用全连接层函数实现多层神经网络并保存和读取模型
使用之前那个格式写法到后面层数多的话会很乱,所以编写了一个函数创建层,这样看起来可读性高点也更方便整理后期修改维护 #全连接层函数 def fcn_layer( inputs, #输入数据 input ...
- mysql学习(4)python操作数据库
整理了一下前面3期学的内容后,现在练习使用python去操作数据库 #!python3# coding:utf-8import pymysqlclass mysql_option(): def __i ...