Mobile development iterator mode learning summary, development mode Summary

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.