The original text was first published on WeChat public account: Jongxingzhi (jzman-blog)
The iterator design pattern is a commonly used design pattern that provides a way to sequentially access various elements in an aggregate object without exposing the internal representation of the object. The iterator design pattern belongs to the behavioral design pattern.
- Related concepts
- scenes to be used
- Java implementation
- Dart implementation
Related concepts
- Abstract aggregation role: define methods for adding, deleting, and other operations on aggregated objects, as well as methods for creating iterators;
- Concrete aggregation role: the concrete realization class of the abstract aggregation role;
- Abstract iterator role: define iterator related methods, such as hasNxt, first, last and other methods;
- Concrete iterator role: the concrete implementation class of the abstract iterator role.
scenes to be used
The usage scenarios of the iterator design pattern are as follows:
- Provide a traversal method for a certain specific data structure defined;
- In order to hide the implementation details of aggregate objects, you can use the iterator design pattern;
- When there are multiple aggregate objects coexisting, the iterator design pattern can shield this difference due to different data structures;
Java implementation
Simply implement the iterator design pattern and define the abstract aggregation role as follows:
/**
* 抽象聚合角色
* Created by jzman
* Powered by 2019/12/22.
*/
interface Aggregate {
Iterator createIterator();
}
Define the abstract iterator role as follows:
/**
* 抽象迭代器角色
* Created by jzman
* Powered by 2019/12/22.
*/
interface Iterator {
Object first();
Object next();
boolean isDone();
Object currentItem();
}
The specific aggregation roles and specific iterator roles are as follows:
/**
* 具体的聚合角色
* Created by jzman
* Powered by 2019/12/22.
*/
class ConcreteAggregate implements Aggregate {
private List<Object> mList;
public ConcreteAggregate(List<Object> mList) {
this.mList = mList;
}
@Override
public Iterator createIterator() {
return new ConcreteIterator();
}
/**
* 具体的迭代器角色
*/
private class ConcreteIterator implements Iterator {
private int mCursor;
@Override
public Object first() {
return mList.get(0);
}
@Override
public Object next() {
mCursor++;
if (mCursor < mList.size()) {
return mList.get(mCursor);
}
return null;
}
@Override
public boolean isDone() {
return mCursor >= mList.size();
}
@Override
public Object currentItem() {
return mList.get(mCursor);
}
}
}
Test the above code:
/**
* 迭代器测试
* Created by jzman
* Powered by 2019/12/22.
*/
class Client {
public static void main(String[] args) {
List<Object> objects = new ArrayList<>();
objects.add("A");
objects.add("B");
objects.add("C");
ConcreteAggregate aggregate = new ConcreteAggregate(objects);
// 获取迭代器
Iterator iterator = aggregate.createIterator();
// 遍历
while (!iterator.isDone()) {
Object o = iterator.currentItem();
System.out.println("data:" + o);
iterator.next();
}
}
}
The test results are as follows:
data:A
data:B
data:C
Dart implementation
main(){
List<Object> mList = ["A","B","C"];
Aggregate aggregate = new ConcreteAggregate(mList);
Iterator iterator = aggregate.createIterator();
while (!iterator.isDone()) {
Object obj = iterator.currentItem();
print("data:${obj.toString()}");
iterator.next();
}
}
// 抽象迭代器角色
abstract class Iterator{
Object first();
Object next();
bool isDone();
Object currentItem();
}
//具体迭代器角色
class ConcreteIterator implements Iterator{
int mCursor = 0;
List<Object> mList;
ConcreteIterator(this.mList);
@override
Object first() {
return mList[0];
}
@override
Object next() {
mCursor++;
if (mCursor < mList.length) {
return mList[mCursor];
}
return null;
}
@override
bool isDone() {
return mCursor >= mList.length;
}
@override
Object currentItem() {
return mList[mCursor];
}
}
// 抽象聚合角色
abstract class Aggregate {
Iterator createIterator();
}
// 具体聚合角色
class ConcreteAggregate implements Aggregate{
List<Object> mList;
ConcreteAggregate(this.mList);
@override
Iterator createIterator(){
return new ConcreteIterator(mList);
}
}
The test results are as follows:
data:A
data:B
data:C
Although the iterator design pattern is often used when traversing collections, it may not have any design pattern concept when used. Such a concept will subtly improve your coding ability.