Design patterns learning summary (xiv) - iterator pattern

definition

The so-called iterative mode is to provide a method for accessing a sequential polymerization of the respective elements of the object, instead of showing the inside thereof is exposed. In the actual development process, we may be required for different needs, you may need a different way to traverse the entire integration of the object, but we do not want filled with a variety of convenient operations in the abstract interface layer polymeric object. This time we need such a thing, it should have the following three functions:

  • You can traverse an aggregate object.
  • Not need to know the internal structure of the aggregate object.
  • We can provide a variety of different ways to traverse.

These three functions are iterators mode need to be addressed. As a powerful model, iterative mode between the elements of the responsibility to walk the iterator, rather than aggregate objects. Doing so simplifies the interface and implementation polymerization, the polymerization can also make it more focused on the things that should be focused, to do so it would be more in line with the principle of single responsibility.

Iterator pattern is a collection of symbiosis with the dead, in general, as long as we achieve a collection, you need to provide this collection's iterator, as javain Collection,List、Set、Map, these collections have their own iterators. If we are to achieve such a new container, of course, we need to introduce an iterative mode, give us the container to achieve an iterator.

Character

  • Iterator: Abstract iterator: all iterators are required to implement an interface between an object to provide a method walk elements polymerization.

  • ConcreteIterator: concrete iterator. With this particular iterator that traverse the object of the specific polymerization. Each polymerization should correspond to a particular object iterator.

  • Aggregate: abstract class polymerization.

  • ConcreteAggregate: class specific polymerization. Realization creatorIterator()method, which returns an iterator object polymerization.

Advantages and disadvantages

advantage:

  • It simplifies traversal

  • Can provide a variety of ways to traverse, for example, ordered list, we can provide a positive sequence needed to traverse, descending traverse two iterative, the user only needs to get together with us to achieve good iterator, you can easily to collection traversed.

  • Good package, users just need to get the iterator can traverse, and for the traversal algorithm then not to care.

Disadvantages:

  • For relatively simple traversal (like array or ordered lists), iterators way to traverse more complicated, like ArrayList, we would rather prefer to use forloops and getmethods to traverse the collection.

Examples

Affirming iterator interface:

public interface Iterator {
    boolean hasNext();
    Object next();
}

Achieve concrete iterator class:

/**
 * 电影
 */
public class FilmIterator implements Iterator {

    private String[] filmItems;
    private int position = 0;

    public FilmIterator(String[] filmItems){
        this.filmItems = filmItems;
    }

    @Override
    public boolean hasNext() {
        if(position > filmItems.length-1 || filmItems[position] == null){
            return false;
        }
        return true;
    }

    @Override
    public Object next() {
        String film = filmItems[position];
        position ++;
        return film;
    }

}

/**
 * 电视频道
 */
public class TVChanneIterator implements Iterator {

    private List<String> tvChanneList;
    private int position = 0;

    public TVChanneIterator(List<String> tvChanneList) {
        this.tvChanneList = tvChanneList;
    }

    @Override
    public boolean hasNext() {
        if (position > tvChanneList.size() - 1 || tvChanneList.get(position) == null) {
            return false;
        }
        return true;
    }

    @Override
    public Object next() {
        String film = tvChanneList.get(position);
        position++;
        return film;
    }
}

Affirming Program Interface:

public interface Program {

    void addItem(String title);

    Iterator createIrerator();
}

Implement specific program categories:

public class FilmProgram implements Program {
    /**
     * 菜单最大长度
     */
    static final int MAX_ITEMS = 5;
    String[] filmItems;
    int index = 0;

    public FilmProgram() {
        filmItems = new String[MAX_ITEMS];
        addItem("大圣归来");
        addItem("大圣又归来");
        addItem("大圣还归来");
        addItem("大圣来都来了");
        addItem("大圣什么时候走");
    }

    @Override
    public void addItem(String title) {
        if (index > MAX_ITEMS) {
            System.out.println("容量已满....");
            return;
        }
        filmItems[index] = title;
        index++;
    }

    @Override
    public Iterator createIrerator() {
        return new FilmIterator(filmItems);
    }
}

public class TVChanneProgram implements Program{

    private List<String> tvChanneList;

    public TVChanneProgram(){
        tvChanneList = new ArrayList<>();
        addItem("欢乐戏剧人");
        addItem("吐槽大会");
        addItem("奇葩说");
    }
    @Override
    public void addItem(String title) {
        tvChanneList.add(title);
    }

    @Override
    public Iterator createIrerator() {
        return new TVChanneIterator(tvChanneList);
    }
}

transfer:

public static void main(String[] args) {
    TVChanneProgram tvChanneProgram = new TVChanneProgram();
    Iterator irerator1 = tvChanneProgram.createIrerator();
    System.out.println("======显示电视频道======");
    while(irerator1.hasNext()){
        System.out.println(irerator1.next());
    }
    System.out.println("======显示电影======");
    FilmProgram filmProgram = new FilmProgram();
    Iterator irerator2 = filmProgram.createIrerator();
    while(irerator2.hasNext()){
        System.out.println(irerator2.next());
    }
}

Console output:

======显示电视频道======
欢乐戏剧人
吐槽大会
奇葩说
======显示电影======
大圣归来
大圣又归来
大圣还归来
大圣来都来了
大圣什么时候走

Guess you like

Origin www.cnblogs.com/markLogZhu/p/11582629.html