Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 19beb80

Browse files
committed
🎉 iterator pattern
1 parent a45c5fe commit 19beb80

File tree

11 files changed

+647
-0
lines changed

11 files changed

+647
-0
lines changed
Lines changed: 370 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,370 @@
1+
---
2+
title: Java设计模式之Iterator模式
3+
date: 2019年09月02日 21:50:06
4+
tags: [Java,design and pattern]
5+
---
6+
7+
## 学习笔记 : Java设计模式之Iterator模式
8+
9+
### 概述
10+
*迭代器模式 : 提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露该对象的内部表示. 迭代器模式又称( Cursor )模式,它是一种对象行为型模式( Iterator Pattern : Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation )*
11+
12+
### 示例程序-详细版
13+
*下面是一个实现了`Iterator模式`的示例程序,其作用是将书籍信息( Book )放到存储书籍信息的书架( BookSelf )上,并将书的名字按顺序显示出来*
14+
15+
1. *Book : 图书信息类*
16+
```java
17+
package pers.huangyuhui.iterator;
18+
19+
/**
20+
* @project: design-patterns
21+
* @description: Book信息
22+
* @author: 黄宇辉
23+
* @date: 9/2/2019-4:59 PM
24+
* @version: 1.0
25+
* @website: https://yubuntu0109.github.io/
26+
*/
27+
public class Book {
28+
29+
private String name;
30+
31+
public Book(String name) {
32+
this.name = name;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
}
40+
```
41+
42+
2. *Aggregate接口 : 充当集合角色*
43+
```java
44+
package pers.huangyuhui.iterator;
45+
46+
47+
/**
48+
* @project: design-patterns
49+
* @description: 集合
50+
* @author: 黄宇辉
51+
* @date: 9/2/2019-5:00 PM
52+
* @version: 1.0
53+
* @website: https://yubuntu0109.github.io/
54+
*/
55+
public interface Aggregate {
56+
57+
Iterator iterator();
58+
}
59+
```
60+
61+
3. *BookSelf类 : 充当具体的集合角色*
62+
```java
63+
package pers.huangyuhui.iterator;
64+
65+
/**
66+
* @project: design-patterns
67+
* @description: 具体的集合
68+
* @author: 黄宇辉
69+
* @date: 9/2/2019-5:03 PM
70+
* @version: 1.0
71+
* @website: https://yubuntu0109.github.io/
72+
*/
73+
public class BookSelf implements Aggregate {
74+
private Book[] books;
75+
private int last = 0;
76+
77+
public BookSelf(int maxsize) {
78+
this.books = new Book[maxsize];
79+
}
80+
81+
public Book getBookByIndex(int index) {
82+
return books[index];
83+
}
84+
85+
public void appendBook(Book book) {
86+
this.books[last++] = book;
87+
}
88+
89+
public int getLength() {
90+
return last;
91+
}
92+
93+
@Override
94+
public Iterator iterator() {
95+
return new BookShelfIterator(this);
96+
}
97+
}
98+
```
99+
100+
4. *Iterator接口 : 充当迭代器角色*
101+
```java
102+
package pers.huangyuhui.iterator;
103+
104+
/**
105+
* @project: design-patterns
106+
* @description: 迭代器
107+
* @author: 黄宇辉
108+
* @date: 9/2/2019-5:01 PM
109+
* @version: 1.0
110+
* @website: https://yubuntu0109.github.io/
111+
*/
112+
public interface Iterator {
113+
boolean hasNext();
114+
115+
Object next();
116+
}
117+
```
118+
119+
5. *BookShelfIterator类 : 充当具体的迭代器角色*
120+
```java
121+
package pers.huangyuhui.iterator;
122+
123+
/**
124+
* @project: design-patterns
125+
* @description: 集体的迭代器
126+
* @author: 黄宇辉
127+
* @date: 9/2/2019-5:07 PM
128+
* @version: 1.0
129+
* @website: https://yubuntu0109.github.io/
130+
*/
131+
public class BookShelfIterator implements Iterator {
132+
133+
private BookSelf bookSelf;
134+
private int index;
135+
136+
public BookShelfIterator(BookSelf bookSelf) {
137+
this.bookSelf = bookSelf;
138+
this.index = 0;
139+
}
140+
141+
@Override
142+
public boolean hasNext() {
143+
return index < bookSelf.getLength();
144+
}
145+
146+
@Override
147+
public Object next() {
148+
return bookSelf.getBookByIndex(index++);
149+
}
150+
}
151+
```
152+
153+
6. *Test测试类*
154+
```java
155+
package pers.huangyuhui.iterator;
156+
157+
/**
158+
* @project: design-patterns
159+
* @description: 测试类
160+
* @author: 黄宇辉
161+
* @date: 9/2/2019-5:09 PM
162+
* @version: 1.0
163+
* @website: https://yubuntu0109.github.io/
164+
*/
165+
public class Test {
166+
167+
//demo
168+
public static void main(String[] args) {
169+
BookSelf bookSelf = new BookSelf(5);
170+
bookSelf.appendBook(new Book("A-BOOK"));
171+
bookSelf.appendBook(new Book("B-BOOK"));
172+
bookSelf.appendBook(new Book("C-BOOK"));
173+
bookSelf.appendBook(new Book("D-BOOK"));
174+
bookSelf.appendBook(new Book("E-BOOK"));
175+
Iterator iterator = bookSelf.iterator();
176+
while (iterator.hasNext()) {
177+
Book book = (Book) iterator.next();
178+
System.out.println(book.getName());
179+
}
180+
}
181+
}
182+
```
183+
184+
7. *程序运行结果如下所示 :*
185+
```
186+
A-BOOK
187+
B-BOOK
188+
C-BOOK
189+
D-BOOK
190+
E-BOOK
191+
```
192+
193+
#### 示例程序的类图
194+
![ ](https://raw.githubusercontent.com/YUbuntu0109/YUbuntu0109.github.io/HexoBackup/source/_posts/Java%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E4%B9%8BIterator-Pattern/Iterator1-ClassDiagram.png)
195+
196+
#### Iterator模式中的角色分析
197+
* `Iterator( 迭代器 )` : 该角色负责定义按顺序逐个遍历元素的接口( API ),在上述程序中,由`Iterator接口`扮演此角色.
198+
* `ConcreteIterator( 具体的迭代器 )` : 该角色负责实现`Iterator角色`所定义的接口( API ). 在上述程序中,由`BookShelfIterator类`扮演此角色.
199+
* `Aggregate( 集合 / 抽象聚合类 )` : 该角色负责定义创建`Iterator角色`的接口( API ),在上述程序中,由`Aggregate接口`扮演此角色.
200+
* `ConcreteAggregate( 具体的集合 / 具体聚合类 )` : 该角色负责实现`Aggregate角色`所定义的接口( API ),它会创建出具体的`Iterator角色`. 在上述程序中,由`BookSelf类`扮演了此角色.
201+
202+
203+
204+
### 示例程序-简洁版
205+
*下面同样是一个实现了`Iterator模式`的示例程序( 同上 ),其作用是将商品数据存储到商品集合( ProductList )中,并将商品的名字按顺序显示出来*
206+
207+
1. *AbstractObjectList : 抽象聚合类*
208+
```java
209+
package pers.huangyuhui.iterator2;
210+
211+
import java.util.List;
212+
213+
/**
214+
* @project: design-patterns
215+
* @description: 抽象聚合类
216+
* @author: 黄宇辉
217+
* @date: 9/2/2019-5:51 PM
218+
* @version: 1.0
219+
* @website: https://yubuntu0109.github.io/
220+
*/
221+
public abstract class AbstractObjectList {
222+
223+
protected List<Object> objects;
224+
225+
public AbstractObjectList(List<Object> objects) {
226+
this.objects = objects;
227+
}
228+
229+
public void addObject(Object object) {
230+
this.objects.add(object);
231+
}
232+
233+
public void removeObject(Object object) {
234+
this.objects.remove(object);
235+
}
236+
237+
public List<Object> getObjects() {
238+
return objects;
239+
}
240+
241+
//创建迭代器的抽象工厂方法
242+
public abstract AbstractIterator iterator();
243+
244+
}
245+
```
246+
247+
2. *ProductList : 商品数据类,充当具体聚合类. ProductIterator(内部类) : 商品迭代器,充当具体迭代器*
248+
```java
249+
package pers.huangyuhui.iterator2;
250+
251+
import java.util.List;
252+
253+
/**
254+
* @project: design-patterns
255+
* @description: 商品书籍类, 充当具体聚合类
256+
* @author: 黄宇辉
257+
* @date: 9/2/2019-5:56 PM
258+
* @version: 1.0
259+
* @website: https://yubuntu0109.github.io/
260+
*/
261+
public class ProductList extends AbstractObjectList {
262+
263+
public ProductList(List<Object> objects) {
264+
super(objects);
265+
}
266+
267+
@Override
268+
public AbstractIterator iterator() {
269+
return new ProductIterator();
270+
}
271+
272+
/*
273+
ProductIterator:商品迭代器,充当具体迭代器(使用内部类实现迭代器,与jdk中的迭代器实现原理相同)
274+
*/
275+
public class ProductIterator implements AbstractIterator {
276+
277+
private int cursor;
278+
279+
public ProductIterator() {
280+
cursor = 0;
281+
}
282+
283+
@Override
284+
public boolean hasNext() {
285+
return cursor < objects.size();
286+
}
287+
288+
@Override
289+
public Object next() {
290+
return objects.get(cursor++);
291+
}
292+
}
293+
294+
}
295+
```
296+
297+
3. *AbstractIterator : 抽象迭代器*
298+
```java
299+
package pers.huangyuhui.iterator2;
300+
301+
/**
302+
* @project: design-patterns
303+
* @description: 抽象迭代器
304+
* @author: 黄宇辉
305+
* @date: 9/2/2019-5:55 PM
306+
* @version: 1.0
307+
* @website: https://yubuntu0109.github.io/
308+
*/
309+
public interface AbstractIterator {
310+
311+
boolean hasNext();
312+
313+
Object next();
314+
}
315+
```
316+
317+
4. *Test : 测试类*
318+
```java
319+
package pers.huangyuhui.iterator2;
320+
321+
import java.util.ArrayList;
322+
import java.util.List;
323+
324+
/**
325+
* @project: design-patterns
326+
* @description: 测试类
327+
* @author: 黄宇辉
328+
* @date: 9/2/2019-6:13 PM
329+
* @version: 1.0
330+
* @website: https://yubuntu0109.github.io/
331+
*/
332+
public class Test {
333+
334+
//demo
335+
public static void main(String[] args) {
336+
List<Object> arrayList = new ArrayList<>();
337+
arrayList.add("A-BOOK");
338+
arrayList.add("B-BOOK");
339+
arrayList.add("C-BOOK");
340+
341+
ProductList productList = new ProductList(arrayList);
342+
productList.addObject("D-BOOK");
343+
productList.addObject("E-BOOK");
344+
productList.removeObject("A-BOOK");
345+
346+
AbstractIterator iterator = productList.iterator();
347+
while (iterator.hasNext()) {
348+
System.out.println(iterator.next());
349+
}
350+
System.out.println("all datas : " + productList.getObjects().toString());
351+
352+
}
353+
}
354+
```
355+
356+
5. *程序运行结果如下所示 :*
357+
```
358+
B-BOOK
359+
C-BOOK
360+
D-BOOK
361+
E-BOOK
362+
all datas : [B-BOOK, C-BOOK, D-BOOK, E-BOOK]
363+
```
364+
365+
#### 示例程序的类图
366+
![ ](https://raw.githubusercontent.com/YUbuntu0109/YUbuntu0109.github.io/HexoBackup/source/_posts/Java%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E4%B9%8BIterator-Pattern/Iterator2-ClassDiagram.png)
367+
368+
369+
370+
*📚参与书籍 : 《图解设计模式 - []结诚浩 · 著》, 《Java设计模式 - 刘伟编 · 著》*
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package pers.huangyuhui.iterator_pattern.iterator1;
2+
3+
4+
/**
5+
* @project: design-patterns
6+
* @description: 集合
7+
* @author: 黄宇辉
8+
* @date: 9/2/2019-5:00 PM
9+
* @version: 1.0
10+
* @website: https://yubuntu0109.github.io/
11+
*/
12+
public interface Aggregate {
13+
Iterator iterator();
14+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /