Jane said design pattern - iterative mode

First, what is the iterator pattern

  Iterator word had appeared in Java, the Java Iterator iterator used to traverse the collection, but the iterator pattern be a pattern of decline, virtually no one would write an iterator alone, unless it is the nature of product development.

  Iterative mode (the Iterator) , there is provided a method for accessing a sequential polymerization of the respective object elements, without exposing the interior of the object representation. UML structure is as follows:

  Which, Aggregate is gathered abstract class, it is responsible for providing concrete iterator role to create the interface; Iterator iterator is an abstract class that defines the object to get the start, to get the next object to determine whether to end the current method of abstract objects, etc., unified interface; ConcreteAggregate aggregate concrete class that inherits aggregate; ConcreteIterator specific iterator class, the iterator succession, beginning achieved, next, whether the end of the current objects or the like.

  1. abstract container

  Is responsible for providing an interface, such as such a method similar createIterator () exist in Java generally iterator () method.

1 public interface Aggregate {
2     
3     public void add(Object object);
4     
5     public void remove(Object object);
6     
7     public Iterator iterator();
8 
9 }

  2. abstract iterator

  Accessing and traversing elements responsible for defining the interfaces, there are basically three methods of fixing, i.e., first () Gets the first element, the next () to access an element, the hasNext () is already traversed in the end portion.

. 1  public  interface the Iterator {
 2      
. 3      public Object Next ();     // iterate to the next element 
. 4      
. 5      public  Boolean the hasNext ();     // if has traversed to the end 
. 6      
. 7      public  Boolean Remove ();     // delete the element currently points 
. 8  
. 9 }

  3. Specific container

 1 public class ConcreteAggregate implements Aggregate {
 2     
 3     private Vector vector = new Vector();
 4 
 5     @Override
 6     public void add(Object object) {
 7         this.vector.add(object);
 8     }
 9 
10     public void remove(Object object) {
11         this.remove(object);
12     }
13 
14     @Override
15     public Iterator iterator() {
16         return new ConcreteIterator(this.vector);
17     }
18     
19 }

  4. DETAILED iterator

  Simple implementation is through a cursor, roll up and down in a container, traversing all the elements it needs to see.

 1 public class ConcreteIterator implements Iterator {
 2     
 3     private Vector vector = new Vector();
 4     public int cursor = 0;    //定义当前游标
 5     
 6     public ConcreteIterator(Vector vector) {
 7         this.vector = vector;
 8     }
 9 
10     @Override
11     public Object next() {
12         Object result = null;
13         
14         if (this.hasNext()) {
15             result = this.vector.get(this.cursor ++);
16         } else {
17             result = null;
18         }
19         
20         return result;
21     }
22 
23     @Override
24     public boolean hasNext() {
25         if (this.cursor == this.vector.size()) {
26             return false;
27         }
28         
29         return true;
30     }
31 
32     @Override
33     public boolean remove() {
34         this.vector.remove(this.cursor);
35         
36         return true;
37     }
38 
39 }

  5. Client Client

  The following test, pay attention to the introduction of a custom Iterator class, rather than a packaged Java Iterator class.

 1 public class Client {
 2     
 3     public static void main(String[] args) {
 4         Aggregate aggregate = new ConcreteAggregate();
 5         aggregate.add("abc");
 6         aggregate.add("aaa");
 7         aggregate.add("1234");
 8         
 9         //遍历
10         Iterator iterator = aggregate.iterator();
11         while (iterator.hasNext()) {
12             System.out.println(iterator.next());
13         }
14     }
15 
16 }

  Results are as follows:

  

Second, the application of iterative mode

  1. When to Use

  • When the aggregate object traversing a

  2. Methods

  • Responsibility between elements of the walk to the iterator, rather than aggregate objects

  3. advantage

  • Support traversing a different way aggregate object
  • Iterator class simplifies the polymerization
  • In the same polymerization may have a plurality of traverse
  • Add a new class of polymeric iterator class and easy, without having to modify the existing code.

  4. shortcomings

  • It increases the complexity of the system. Since the separation of duties iterative mode and stores data through the data, increasing the need for new polymerization of a corresponding increase in new class iterator class, increases the complexity of the system.

  5. usage scenarios 

  • Accessing a content of the aggregate object without exposing its internal representation when
  • When the need to provide a variety of convenient ways to aggregate objects
  • Traversing the different polymeric structure to provide a unified interface

  6. Application Examples

  • The Java Iterator iterator
  • foreach traversal

Third, to achieve the iterator pattern

  This part will not repeat, and the specific implementation is not much difference in the code above, can be varied depending on the specific application scenario, of course, also read java.util.Iterator source.

 


  This common 23 design patterns on the introduction is over, all have switched source code uploaded to the cloud, as (design patterns beyond the usual 23 design patterns) extension of the new model, there is the opportunity to follow-up will continue to be updated in a short time no longer content to write about design patterns.

 

  Source Address: https://gitee.com/adamjiangwh/GoF 

Reproduced in: https: //www.cnblogs.com/adamjwh/p/11024311.html

Guess you like

Origin blog.csdn.net/weixin_34337265/article/details/93160840
Recommended