Mobile development iterator mode learning summary, development mode Summary
I. Concepts
An aggregate object has two responsibilities: one is to store internal data and the other is to traverse internal data. From the perspective of dependency, the former is the fundamental attribute of the aggregate object, while the latter can be changed and separated. The aggregate object traversal behavior is extracted as an iterator that specifically provides the behavior of traversing the aggregate object's internal data. Here is the iterator.
Ii. Category chart
3. Product Name traversal for instances
Code:
/** Abstract iterator class */public interface extends actiterator {public void next (); public boolean isLast (); public void previous (); public boolean isFirst (); public String getNextItem (); public String getpreviusitem ();}
// Abstract product set: 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 actiterator getIterator ();}
// Specific iterator class public class MyIterator implements extends actiterator {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
-1) {index2 -- ;}@ Override public boolean isFirst () {return (index2 =-1) ;}@ Override public String getNextItem () {return productsName [index1] ;}@ Override public String getpreviusitem () {return productsName [index2] ;}}
// Specific product class public class ProductList extends AbstractProductList {public ProductList (String [] productsName) {super (productsName) ;}@ Override public extends actiterator getIterator () {return new MyIterator (this );}}
/* Client */public class Client {public static void main (String [] args) {String [] productNames = {"ThinkPad", "Tissot watch ", "iPhone", "LV handbag"}; AbstractIterator iterator; AbstractProductList productList; productList = new ProductList (productNames); iterator = productList. getIterator (); // obtain its corresponding iteration class while (! Iterator. isLast () {// use the iterator class to iterate System. out. println (iterator. getNextItem (); iterator. next ();} System. out. println ("---------------------------------"); while (! Iterator. isFirst () {System. out. println (iterator. getpreviusitem (); iterator. previous ();}}}
Iv. Summary
Time to use:
You need to access the internal content of an aggregate object without exposing its internal representation, or provide multiple Iteration Methods for the aggregate object, and provide a unified interface for Traversing different aggregate structures.