cool hit counter Iterator pattern and the use of inner classes_Intefrankly

Iterator pattern and the use of inner classes


I.

(a) The previous article wrote about the role of static, in part by introducing inner classes and static inner classes, and the following is an introduction to the benefits of inner classes in the context of the iterator pattern in design patterns.

1.To begin with the role of internal classes will be stated.

First, the internal class can directly access the external class of things, if there is no internal class, to implement the iterator pattern, there are 2 ways, first, in the container class to define some methods, then you need to define some global member variables to record the cursor and so on some data, resulting in a cumbersome container class, not easy to extend. Second, creating an iterator class on a level with the container class requires class-to-class communication, raising the coupling between the container class and the iterator class.

   Internal classes, on the other hand, have direct access to the member variables and member functions of the container class, and the internal classes simply implement their own methods.

   Another advantage is that, as one commenter said, ArrayList's inner class ArrayIterator is rarely reused by other classes, which is why they designed it as an inner class, and it's not a good idea to design it as an inner class if it's going to be reused a lot by other classes, even with all its "advantages". HashMap's internal class Entry is typically designed for this reason, the first is that it wants to use the external class Entry are not available, there is already a class of the same name, and it so happens that HashMap's Entry is also basically only HashMap itself will use, that just designed as an internal class.

2.Benefits of the iterator pattern.

  • Simplified traversal, for the object collection traversal, or more trouble, for the array or ordered list, we can still get through the cursor, but the user needs to traverse the object on their own with a good understanding of the set, but for the hash table, the user traversal is more trouble. And with the introduction of the iterator method, it's much easier for the user to use.
  • We can provide a variety of traversal methods, for example, for ordered lists, we can provide two kinds of iterators according to the need for positive-order traversal, inverted-order traversal, the user only needs to get our implemented iterator, you can easily traverse the collection.
  • Encapsulation is good, the user only needs to get the iterator to traverse, and doesn't have to care about the traversal algorithm.
  • When adding other collection types, the corresponding interface is implemented directly, and the internal class implements the Iterator interface directly, realizing all its related methods, in full compliance with the open-closed principle (my own idea).

II. UML diagrams

The following code is a stripped-down version of the Java collection framework's internal implementation of the iterator pattern.

public interface Iterator<E> {//Iterator interface lite
    boolean hasNext();
    E next();
}
public interface List<E> {// Container Interface Lite
    Iterator<E> iterator();
}
public class ArrayList<E> implements List<E> {
    private Object[] array;
    
    public ArrayList(){
        this.array=new Object[5];
        this.array[0]="a";
        this.array[1]="b";
        this.array[2]="c";
        this.array[3]="d";
        this.array[4]="e";
    }
    public Iterator<E> iterator() {
        return new ArrayIterator();
    }
    public class ArrayIterator implements Iterator<E>{ // Internal classes that implement other class interfaces, Reduced coupling   Must be achievedIterator Methods to be available
        private int cursor=0;
        public boolean hasNext() {
            if(cursor<array.length){ // It is possible to directly access the external classObject arrays
                return true;
            }
            return false;
        }
        @SuppressWarnings("unchecked")
        public E next() {
            E e=(E) array[cursor++];            
             return e;  // return the data corresponding to the external class directly
        }
    }
}

Recommended>>
1、Pros and Cons of Adopting Istio Service Grid Running on Kubernetes
2、Chinas most powerful concept car driverless without windows charging ten minutes range 600Km scare the Yanks
3、iPhone unlocking device maker Grayshift suffers brief data breach
4、The first thing you need to do is to get a good idea of what youre doing
5、The THERO that guards online privacy cybersecurity depends on this ball

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号