1, Concept
An aggregate object has two responsibilities, one is to store internal data, the other is to traverse internal data. From the perspective of dependency, the former is the fundamental attribute of aggregate object, while the latter can be changed and separated. The traversal behavior of the aggregate object is extracted as an iterator, which specifically provides the behavior of traversing the internal data of the aggregate object. Here is the iterator.
2, Class diagram
3, Commodity name traversal of instance
code:
/*
* Abstract iterator class
*/
public interface AbstractIterator {
public void next();
public boolean isLast();
public void previous();
public boolean isFirst();
public String getNextItem();
public String getPreviousItem();
}
//Abstract commodity collection: Abstract aggregation class
public abstract class AbstractProductList {
private String []productsName;//Product name array
public AbstractProductList(String []productsName){
this.productsName=productsName;
}
public String[] getProductsName(){
return this.productsName;
}
public abstract AbstractIterator getIterator();
}
//Concrete iterator class
public class MyIterator implements AbstractIterator{
private String []productsName;
private int index1;
private int index2;
public MyIterator(AbstractProductList list) {
productsName=list.getProductsName();
index1=0;
index2=productsName.length-1;
}
@Override
public void next() {
if(index1<productsName.length){
index1++;
}
}
@Override
public boolean isLast() {
return (index1==productsName.length);
}
@Override
public void previous() {
if(index2>-1){
index2--;
}
}
@Override
public boolean isFirst() {
return (index2==-1);
}
@Override
public String getNextItem() {
return productsName[index1];
}
@Override
public String getPreviousItem() {
return productsName[index2];
}
}
//Specific commodities
public class ProductList extends AbstractProductList{
public ProductList(String[] productsName) {
super(productsName);
}
@Override
public AbstractIterator getIterator() {
return new MyIterator(this);
}
}
/*
client
*/
public class Client {
public static void main(String[] args) {
String []productNames={"ThinkPad Computer","Tissot Wrist Watch","iPhone Mobile phone",
"LV Handbag"};
AbstractIterator iterator;
AbstractProductList productList;
productList=new ProductList(productNames);
iterator=productList.getIterator();//Get its corresponding iteration class
while(!iterator.isLast()){//Using iterator class to iterate
System.out.println(iterator.getNextItem());
iterator.next();
}
System.out.println("---------------------------------");
while(!iterator.isFirst()){
System.out.println(iterator.getPreviousItem());
iterator.previous();
}
}
}
4, Summary
Use time:
When you need to access the internal content of an aggregation object without exposing its internal representation, or provide multiple iterations for the aggregation object, and provide a unified interface for traversing different aggregation structures.
We use our time to get rid of others' expectations and find ourselves.