Programmers: I finally know what the iterator pattern

Author: Thousand absolutely

E-mail: [email protected]

Public number: Thousand absolutely

Title: Design Patterns Iterator mode (reproduced please indicate the source)

Hello, Tell me what you gentlemen, thousands never came back I updated recently undergone a major event in life, led me to dove for a week, that is, one thousand a week never saw the room, and finally by their own efforts to help my family I buy a house (hahaha) Tell me what you believe all of you gentlemen can.

Well, I do not joke, because thousand of never getting married at the end, so take advantage now go read about the marriage room, I feel like I buy a house efficiency is very fast, it is estimated met my sales are fun to fly, I Thursday home sales contact at night, Saturday morning to see the room in the afternoon put the down payment paid. My wife say too fast, and I feel the rhythm very quickly, so when the house is tied leeks, after no longer want to eat anything eat anything, and from the back of the mortgage.

Well now return to the topic (end text has eggs) in front of a one thousand never introduced the observer pattern, classic look Tell me what you do not know the how, there is no observer mode, there has been little new understanding of it, if there is if then the purpose of reached one thousand never, never will explain this one thousand design mode iterative mode, although this mode is now a high probability that we will not write their own, because the java has done very perfect, but there is an old saying, study history, we can better see the direction of the front line.

This article must be one thousand to explain through an example of what is an iterative mode.

definition

First, we refer to the practice content Baidu encyclopedia

Iterative mode (the Iterator), there is provided a method of sequential access an object in the polymerization of various elements, without exposing the interior of the object representation.

One thousand absolute understanding of this definition is: iterator pattern in order not to expose internal object using what data structure, so you can order an external access to the object inside the element method

Understand or can understand, if you can not understand the words above, thousands must be to explain to you in code.

Code

Define an iterator interface

public interface Iterator<T> {
    //返回该对象的第一个元素
    T first();
    //返回该对象的下一个元素
    T next();
    //判断该对象有没有下一个元素
    boolean hasNext();
    //返回该对象现在的元素
    T getCurrentObj();
}
复制代码

Define a class that implements the iterator

public class ConcreateIterator<T> implements Iterator<T> {
    List<T> list ;
    int current =0;
    public ConcreateIterator(List<T> list){
        this.list = list;
    }

    @Override
    public T first() {
        return list.get(0);
    }

    @Override
    public T next() {
        T t = null;
        if(this.hasNext()){
            t = list.get(current);
            current ++ ;
        }
        return t;
    }

    @Override
    public boolean hasNext() {
        if(current < list.size()){
            return  true;
        }
        return false;
    }

    @Override
    public T getCurrentObj() {
        return list.get(current);
    }
}
复制代码

Define an aggregate interface

public  interface Aggregate<T> {
    //添加一个对象
    void add(T t);
    //删除一个对象
    void remove(T t);
    //生成遍历器
    Iterator<T> createIteratory();
}
复制代码

Define a class that implements an interface polymerization

public class ConcreateAggregate<T> implements Aggregate<T> {
    private List<T> list = new ArrayList();

    @Override
    public void add(T t) {
        list.add(t);
    }

    @Override
    public void remove(T t) {
        list.remove(t);
    }

    @Override
    public Iterator createIteratory() {
        return new ConcreateIterator(list);
    }
}
复制代码

Define a test class

public class TestIterator {
    public static void main(String []args){
        ConcreateAggregate<String>  concreateAggregate= new ConcreateAggregate<String>();
        concreateAggregate.add("测试");
        concreateAggregate.add("测试1");
        concreateAggregate.add("测试2");
        concreateAggregate.add("测试3");
        concreateAggregate.add("测试4");
        concreateAggregate.add("测试5");
        concreateAggregate.add("测试6");
        concreateAggregate.remove("测试4");
        Iterator iterator = concreateAggregate.createIteratory();
        System.out.println("第一个是:" + iterator.first());
        System.out.println("当前的元素是: " + iterator.getCurrentObj());
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
复制代码

Here Insert Picture Description

to sum up

Tell me what you see here believe gentlemen think the above code is very familiar with it, yes one thousand absolutely is an example of realization of the iterator pattern in the reference jdk, java iterators now use has been very good, we now basically do not have to write the iterator, and there may never write now is thousands of code is too simple, not encountered cases write their own iterator, so the introduction of a little rough but please Tell me what gentlemen do not mind.

Next Issue: prototype model.

If you have a favorite one thousand absolutely can follow, comment, forwarding it.

Finally, what do you think we should start a new house smart home can improve the quality of life yet.

Here Insert Picture Description

Guess you like

Origin juejin.im/post/5e79c96ae51d4527151585c4