Iterator mode of Java design pattern

Source: Internet
Author: User

 Category: "java"2013-07-15 10:58 917 people read reviews (0) favorite reports The so-called iterator mode, that is, iterator provides a unified access for different containers. In this paper, we use the container in Java as an example to simulate the principle of iterator.
Reference: Horse soldier java design mode Iterator
1. First define a container collection interface.
[Java]View Plaincopy
    1. Package com.njupt.zhb.learn.iterator;
    2. Public interface Collection {
    3. void Add (Object o);
    4. int size ();
    5. Iterator Iterator ();
    6. }

2. Define an interface for an iterator iterator
[Java]View Plaincopy
    1. Package com.njupt.zhb.learn.iterator;
    2. Public interface Iterator {
    3. Object next ();
    4. boolean hasnext ();
    5. }

3. Define a ArrayList, implement the collection interface, and write an inner class that implements the iterator interface.
[Java]View Plaincopy
  1. Package com.njupt.zhb.learn.iterator;
  2. Import com.njupt.zhb.learn.iterator.Collection;
  3. Public class ArrayList implements Collection {
  4. Object[] objects = new object[10];
  5. int index = 0;
  6. public Void Add (Object o) {
  7. if (index = = objects.length) {
  8. object[] newobjects = new Object[objects.length * 2];
  9. System.arraycopy (objects, 0, Newobjects, 0, objects.length);
  10. objects = newobjects;
  11. }
  12. Objects[index] = o;
  13. Index + +;
  14. }
  15. public int size () {
  16. return index;
  17. }
  18. Public Iterator Iterator () {
  19. return new Arraylistiterator ();
  20. }
  21. private class Arraylistiterator implements Iterator {
  22. private int currentindex = 0;
  23. @Override
  24. Public Boolean hasnext () {
  25. if (currentindex >= index) return false;
  26. Else return true;
  27. }
  28. @Override
  29. Public Object Next () {
  30. Object o = Objects[currentindex];
  31. Currentindex + +;
  32. return o;
  33. }
  34. }
  35. }


4. Write the test procedure Testmain
[Java]View Plaincopy
  1. Package com.njupt.zhb.learn.iterator;
  2. Import com.njupt.zhb.learn.iterator.ArrayList;
  3. Public class Testmain {
  4. public static void Main (string[] args) {
  5. Collection C = new ArrayList ();
  6. For (int i=0; i<; i++) {
  7. C.add ("string" +i);
  8. }
  9. System.out.println (C.size ());
  10. Iterator it = C.iterator ();
  11. While (It.hasnext ()) {
  12. Object o = It.next ();
  13. System.out.println (o.tostring () + "");
  14. }
  15. }
  16. }

Operation Result:

[HTML]View Plaincopy
    1. 15
    2. String 0
    3. String 1
    4. String 2
    5. String 3
    6. String 4
    7. String 5
    8. String 6
    9. String 7
    10. String 8
    11. String 9
    12. String 10
    13. String 11
    14. String 12
    15. String 13
    16. String 14


As can be seen from the above, design patterns are used everywhere in object-oriented polymorphicinterface calls a function in a subclass。 Source code Download:
http://download.csdn.net/detail/nuptboyzhb/5755295

Iterator mode of Java design pattern

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.