Design Pattern-iterator pattern JAVA Implementation Code

Source: Internet
Author: User

Iterator mode: provides a method to access each element in an aggregate object sequentially without exposing its internal representation.

The iterator mode provides a way to access elements in an aggregate object in sequence without having to know how they are identified internally. In addition, the iterator mode transfers the responsibility for migrating between elements to the iterator, instead of aggregating objects, so that aggregation is more focused on data sets.

Case study: My friends and I went to the supermarket to shop separately. During the checkout, we traversed our "goods scanning" list (print and print the shopping list, me uses arraylist, and friends use the thing [] array, which causes traversal problems.

CaseCode:

Thing. Java

 

 
Public class thing {private string name; private long price; public thing (string name, long price) {This. name = Name; this. price = Price ;}@ overridepublic string tostring () {return "thing [name =" + name + ", price =" + Price + "]" ;}}

Me. Java

 

 

Import Java. util. arraylist; import Java. util. iterator; public class me {private arraylist <thing> shopping = new arraylist <thing> (); Public me () {shopping. add (new thing ("Chocolate", 35); shopping. add (new thing ("herbal tea", 4); shopping. add (new thing ("so", 9); shopping. add (new thing ("banana", 12);} public iterator <thing> createiterator () {return shopping. iterator ();}}

Friends. Java

 

 

 
Import Java. util. iterator; public class friends {private thing [] Shopping = new thing [100]; Public friends () {shopping [0] = new thing ("milk", 15 ); shopping [1] = new thing ("apple", 10); shopping [2] = new thing ("Potato Chips", 4);} public iterator <thing> createiterator () {return New arrayiterator (shopping );}}

Arrayiterator. Java

 

 

Import Java. util. iterator; public class arrayiterator implements iterator <thing> {private thing [] thingarray; private int position = 0; Public arrayiterator (thing [] array) {thingarray = array ;} @ overridepublic Boolean hasnext () {While (position <thingarray. length & thingarray [position]! = NULL) {return true;} return false;} @ overridepublic thing next () {return thingarray [position ++] ;}@ overridepublic void remove () {// noting to do }}

Test. Java

 

 

Import Java. security. allpermission; import Java. util. arraylist; import Java. util. iterator; public class test {public static void main (string ARGs []) {arraylist <iterator <thing> alliterator = new arraylist <iterator <thing> (); me me = new me (); friends = new friends (); iterator <thing> meiterator = me. createiterator (); iterator <thing> friendsiterator = friends. createiterator (); alliterator. add (meiterator); alliterator. add (friendsiterator); For (iterator <thing> iterator: alliterator) {While (iterator. hasnext () {system. out. println (iterator. next ();} system. out. println ("---------------------");}}}

Test results:

 

 

Thing [name = chocolate, price = 35] thing [name = herbal tea, price = 4] thing [name = so, price = 9] thing [name = banana, price = 12] ------------------- thing [name = milk, price = 15] thing [name = apple, price = 10] thing [name = potato chips, price = 4] -------------------

Summary:

Design principles:

Single responsibility: one class should have only one responsibility for change.

When a module or class is designed to support only a set of related functions, we say it has a high cohesion.

 

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.