美文网首页
迭代器模式

迭代器模式

作者: 南汐寒笙已被占用 | 来源:发表于2017-08-29 13:12 被阅读0次

    模式名称:迭代器模式 Iterator
    描述:一个一个遍历
    对代码的作用:当数据容器内存储结构发生改变,不必修改客户端遍历的代码
    容器接口:

    public interface Container<T> {
    
        ContainerIterator<T> iterator();
    
        void add(T element);
    
    }
    

    迭代器接口:

    public interface ContainerIterator<T> { 
          T next(); boolean hasNext(); 
          void     setSize(int size); T prev();
    }
    

    容器实现:

    public class ContainerImpl<T> implements Container<T> {
    
    private Object[] elements;
    
    private int index;
    
    private int size;
    
    private ContainerIterator iterator;
    
    public ContainerImpl() {
        elements = new Object[10];
        iterator = new ContainerIteratorImpl(elements, size);
    }
    
    @Override
    public ContainerIterator iterator() {
        return iterator;
    }
    
    @Override
    public void add(T element) {
        if(index > 9) {
            throw new ArrayIndexOutOfBoundsException();
        }
        elements[index++] = element;
        ++size;
        iterator.setSize(size);
      }
    }
    

    迭代器实现:

    public class ContainerIteratorImpl<T> implements ContainerIterator<T> {
        private int index = 0;
        private Object[] elements;
        private int size;
        public ContainerIteratorImpl(Object[] elements, int size) {
            this.elements = elements;
            this.size = size;
        }
        @Override
        public T next() {
            return (T)elements[index++];
        }
        @Override
        public boolean hasNext() {
            return index < size;
        }
        @Override
        public void setSize(int size) {
            this.size = size;
        }
        @Override
        public T prev() {
            if(index < 1) {
                throw new ArrayIndexOutOfBoundsException();
            }
            return (T) elements[--index];
          }
    }
    

    容器元素:

    public class User {
    
    private String name;
    
    private int age;
    
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    }
    

    客户端:

    public class Main {
    
    public static void main(String[] args) {
        Container<User> userList = new ContainerImpl<>();
        userList.add(new User("小明", 20));
        userList.add(new User("小红", 23));
        userList.add(new User("小刚", 12));
        userList.add(new User("小离", 24));
    
        ContainerIterator<User> iterator = userList.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
    }
    

    UML图:


    总结:当ContainerImpl内部存储结构改变 我们只需要修改迭代器实现类的实现,而无需修改客户端代码,如果只有一两处修改,修改客户端没什么,当使用量特别大的时候,就比较棘手了,只需要修改迭代器实现,一劳永逸

    相关文章

      网友评论

          本文标题:迭代器模式

          本文链接:https://www.haomeiwen.com/subject/ufeodxtx.html