Iterator模式
Iterator英文意思是重复做某件事,中文翻译为迭代器,这个设计模式中主要有Iterator(迭代器),ConcreteIterator(具体的迭代器),Aggergate(集合),ConcreteAggregate(具体的集合)四个角色;下面举一个例子来说明。
对某个书架上的书进行遍历,并把每本书的书名打印到控制台上。
1,首先创建Aggergate和Iterator接口
public interface Aggregate {
public abstract Iterator iterator();
}
public interface Iterator {
public abstract boolean hasNext();
public abstract Object next();
}
对于每个实现Aggergate的实现类,都会有一个方法生成自己的迭代器,每个迭代器都会有hasNext和next方法。
2,创建ConcreteIterator和ConcrereAggregate的类
public class Books {
private String name;
public Books(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
import java.util.ArrayList;
public class BookShelf implements Aggregate {
private ArrayList books;
public BookShelf(int initial) {
this.books = new ArrayList(initial);
}
@Override
public Iterator iterator() {
return new BookShelfIterator(this);
}
public void appendBooks(Books books) {
this.books.add(books);
}
public Books getBookAt(int index) {
return (Books) books.get(index);
}
public int getLength() {
return books.size();
}
}
public class BookShelfIterator implements Iterator {
private BookShelf bookshelf;
private int index;
public BookShelfIterator(BookShelf bookShelf) {
this.bookshelf = bookShelf;
this.index = 0;
}
@Override
public boolean hasNext() {
if (index < bookshelf.getLength()) {
return true;
} else
return false;
}
@Override
public Object next() {
Books book = bookshelf.getBookAt(index);
index++;
return book;
}
}
具体的迭代器使用具体的集合的一个变量作为自己的字段,通过构造函数为具体的集合赋值,具体的集合通过Iterator生成迭代器
3,程序入口
public class Main {
public static void main(String[] args) {
BookShelf bookshelf = new BookShelf(4);
bookshelf.appendBooks(new Books("A"));
bookshelf.appendBooks(new Books("B"));
bookshelf.appendBooks(new Books("C"));
bookshelf.appendBooks(new Books("D"));
bookshelf.appendBooks(new Books("E"));
bookshelf.appendBooks(new Books("F"));
bookshelf.appendBooks(new Books("G"));
bookshelf.appendBooks(new Books("H"));
Iterator it = bookshelf.iterator();
while (it.hasNext()) {
Books book = (Books) it.next();
System.out.println(book.getName());
}
}
}
Iterator设计模式体现了高聚合低耦合的设计思想,相比传统的循环代码,如果具体的集合中字段的储存方式发生了改变,仅需要对具体的集合那个类进行修改,而具体的迭代器和Main不用修改,一般的循环代码无法做到这一点。
Iterator模式的更多相关文章
- 第3月第13天 cpp模版 Iterator模式 proactor
1.模版除了传参,还可以自动创建.而传指针只是传参而已. template <class TYPE, class FUNCTOR, class ACE_LOCK, typename TIME_P ...
- Java 实现迭代器(Iterator)模式
类图 /** * 自己定义集合接口, 相似java.util.Collection * 用于数据存储 * @author stone * */ public interface ICollection ...
- 设计模式之Iterator模式
STL里的iterator就是应用了iterator模式. 一.什么是迭代模式 Iterator模式也叫迭代模式,是行为模式之一,它把对容器中包含的内部对象的访问委让给外部类,使用Iterator按顺 ...
- Java设计模式(12)迭代模式(Iterator模式)
上了这么多年学,我发现一个问题,好象老师都很喜欢点名,甚至点名都成了某些老师的嗜好,一日不点名,就饭吃不香,觉睡不好似的,我就觉得很奇怪,你的课要是讲的好,同学又怎么会不来听课呢,殊不知:“误人子弟, ...
- 设计模式—迭代器Iterator模式
什么是迭代器模式? 让用户通过特定的接口访问容器的数据,不需要了解容器内部的数据结构. 首先我们先模仿集合中ArrayList和LinkedList的实现.一个是基于数组的实现.一个是基于链表的实现, ...
- 设计模式(一)Iterator模式
Iterator模式用于在数据集合中按照顺序遍历集合.即迭代器模式. 下面来看一段实现了迭代器模式的示例程序. 这段程序的作用是将书(Book)放置到书架(BookShelf)中,并将书的名字按顺序显 ...
- 设计模式C++描述----20.迭代器(Iterator)模式
一. 举例说明 我们知道,在 STL 里提供 Iterator 来遍历 Vector 或者 List 数据结构. Iterator 模式也正是用来解决对一个聚合对象的遍历问题,将对聚合的遍历封装到一个 ...
- 【设计模式大法】Iterator模式
Iterator模式 --一个一个遍历 在Java中的for语句中 i++的作用是让 i 的值在每次循环后自增1,这样就可以访问数组中的下一个元素.下下一个元素.再下下一个元素,也就实现了从头至尾逐一 ...
- Java设计模式之Iterator模式
分类: [java]2013-07-15 10:58 917人阅读 评论(0) 收藏 举报 所谓Iterator模式,即是Iterator为不同的容器提供一个统一的访问方式.本文以java中的容器为例 ...
- 1、迭代器 Iterator模式 一个一个遍历 行为型设计模式
1.Iterator模式 迭代器(iterator)有时又称游标(cursor)是程序设计的软件设计模式,可在容器(container,例如链表或者阵列)上遍访的接口,设计人员无需关心容器的内容. I ...
随机推荐
- [题解] CF622F The Sum of the k-th Powers
CF622F The Sum of the k-th Powers 题意:给\(n\)和\(k\),让你求\(\sum\limits_{i = 1} ^ n i^k \ mod \ 10^9 + 7\ ...
- Spring Boot作为Spring Cloud基础设施
spring cloud包含的核心特性: Distributed/versioned configuration(分布式配置) Service registration and discovery(服 ...
- Tomcat跨域
先下载 cors-filter-2.6.jar 2.java-property-utils-1.9.1.jar,这两个文件有些在csdn上积分太高,有些要百度网盘,还要下载百度网盘客户端,太麻烦,直 ...
- Codeforces Round #604 (Div. 2) 部分题解
链接:http://codeforces.com/contest/1265 A. Beautiful String A string is called beautiful if no two con ...
- vue element-ui Table数据解除自动响应方法
在对列表Table进行数据编辑时,会存在table的增删改操作后,列表view也自动响应发生了变化,原因是赋值的数据是一个引用类型共享一个内存区域的.所以我们就不能直接连等复制,需要重新克隆一份新的数 ...
- vue累加计数器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- tensorflow笔记(北大网课实战)
1. tf.multiply(x,y1) # 对应元素相乘 tf.matmul(x,y2) # 矩阵相乘 2.会话:执行计算图中的节点运算的. with tf.Session() as sess: p ...
- rename 修改文件名
Linux的 rename 命令有两个版本,一个是C语言版本的,一个是Perl语言版本的,早期的Linux发行版基本上使用的是C语言版本的,现在已经很难见到C语言版本的了,由于历史原因,在Perl语言 ...
- python 运算符 取余 取商 in not in
#运算符sum = 9//2 #取商print(sum) sum = 9%2 #取余print(sum) #inname1 = '小林'name2 = '林倩'if '林' in name1: pri ...
- javascript编程中极易出现的错误(个人)
2018-08-10 1,setInterval打错字写成ser 2,document.getElementById().innerHTML;HTML需要全部大写 3,在for循环中定义一个i时要记住 ...