文章目录
  1. 1. 简单介绍
  2. 2. UML图
  3. 3. 实例
  4. 4. 输出
  5. 5. 效果

最近在复习体系结构,于是决定整理一发设计模式,今天我们就将整理第一个设计模式—Iterator Pattern(迭代器模式)。接下来我们还会介绍Prototype和Proxy,这些都是集合类型针对接口编程(Programming to Interfaces)的设计模式。

简单介绍

提供一种方法顺序访问一个聚合对象的各个元素,而又不暴露其底层的表示。(Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation)

  1. Supports multiple traversals of aggregate objects.
  2. Provides a uniform interface for traversing different aggregate structures (it supports polymorphic iteration).

UML图

  1. Iterator(迭代器): 定义访问和遍历元素的接口。 对应上图中的 Iterator接口类,定义是否能取到下一个元素的信息的hasNext方法和取得下一个元素的next方法.
  2. ConcreteIterator(具体实现的迭代器):实际上实现Iterator所定义的接口。对应上图中的ConcreteIterator 类,它需要拥有遍历实现所需的必要信息
  3. Aggregate(集合):定义建立Iterator(迭代器)的接口。对应上图中Aggregate 接口类,决定了iterator方法
  4. ConcreteAggregate(具体实现的集合):实际上实现Aggregate所定义的接口。它是实际的Iterator(迭代器),也是ConcreteIterator的对象实例。对应上图中的ConcreteAggregate类,它实现了iterator方法。

实例

  1. Aggregate类

    public interface Aggregate {
        public Iterator createIterator();
    }
    
  2. ConcreteAggregate类

    public class ConcreteAggregate implements Aggregate{
        private ArrayList<User> users;
    
        public ConcreteAggregate() {
            users = new ArrayList<User>();
        }
    
        public User getUser(int index) {
            return users.get(index);
        }
    
        public void addUser(User user) {
            users.add(user);
        }
    
        public int getLength() {
            return users.size();
        }
    
        @Override
        public Iterator createIterator() {
            // TODO Auto-generated method stub
            return new ConcreteIterator(this);
        }
    
    }
    
  3. ConcreteAggregate2类

    public class ConcreteAggregate2 implements Aggregate{
        private User[] users;
        private int last = 0;
    
        public ConcreteAggregate2(int maxSize) {
            users = new User[maxSize];
        }
    
        public User getUser(int index) {
            return users[index];
        }
    
        public void addUser(User user) {
            this.users[last] = user;
            last++;
        }
    
        public int getLength() {
            return users.length;
        }
    
        @Override
        public Iterator createIterator() {
            return new ConcreteIterator2(this);
        }
    
    }
    
  4. Iterator类

    public interface Iterator {
        public Object next();
        public boolean hasNext();
        public void remove();
    }
    
  5. ConcreteIterator类

    public class ConcreteIterator implements Iterator{
        private ConcreteAggregate concreteAggregate;
        private int index;
    
        public ConcreteIterator(ConcreteAggregate concreteAggregate) {
            this.concreteAggregate = concreteAggregate;
        }
    
        @Override
        public Object next() {
            return concreteAggregate.getUser(index++);
        }
    
        @Override
        public boolean hasNext() {
            if(index >= concreteAggregate.getLength() || null == concreteAggregate.getUser(index)) {
                return false;
            }
            return true;
        }
    
        @Override
        public void remove() {
            if(index<=0){
    
            }
        }
    
    }    
    
  6. ConcreteIterator2类

    public class ConcreteIterator2 implements Iterator{
        private ConcreteAggregate2 concreteAggregate2;
        private int index;
    
        public ConcreteIterator2(ConcreteAggregate2 concreteAggregate2) {
            this.concreteAggregate2 = concreteAggregate2;
        }
    
        @Override
        public Object next() {
            return concreteAggregate2.getUser(index++);
        }
    
        @Override
        public boolean hasNext() {
            if(index >= concreteAggregate2.getLength() || null == concreteAggregate2.getUser(index)) {
                return false;
            }
            return true;
        }
    
        @Override
        public void remove() {
            // TODO Auto-generated method stub
    
        }
    
    }
    
  7. User类

    public class User {
        private int id;
        private String name;
        private int age;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        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;
        }
        public User(int id, String name, int age) {
            super();
            this.id = id;
            this.name = name;
            this.age = age;
        }
    }
    
  8. Client类

    public class Client {
        public static void main(String arg[]) {
            User user1 = new User(1, "never", 1);
            User user2 = new User(2, "2", 2);
            User user3 = new User(3, "3", 3);
            User user4 = new User(4,"4",4);
             ConcreteAggregate concreteAggregate = new ConcreteAggregate();
            concreteAggregate.addUser(user1);
            concreteAggregate.addUser(user2);
            concreteAggregate.addUser(user3);
            concreteAggregate.addUser(user4);
    
            Iterator iterator = concreteAggregate.createIterator();
            while(iterator.hasNext()) {
                User user = (User) iterator.next();
                System.out.println(user.getName());
            }
    
            ConcreteAggregate2 concreteAggregate2 = new ConcreteAggregate2(4);
            concreteAggregate2.addUser(user4);
            concreteAggregate2.addUser(user3);
            concreteAggregate2.addUser(user2);
            concreteAggregate2.addUser(user1);
    
            Iterator iterator2 = concreteAggregate2.createIterator();
            while(iterator2.hasNext()) {
                User user = (User) iterator2.next();
                System.out.println(user.getName());
            }
        }
    }
    

输出

never
2
3
4
4
3
2
never

效果

  1. 针对接口编程和信息隐藏(Programming to interfaces and Information Hiding)
  2. 支持一个结合遍历方式的变化(It supports variations in the traversal of an aggregate。For example, code generation may traverse the parse tree inorder or preorder. Iterators make it easy to change the traversal. Just replace the iterator instance with a different one)
  3. 迭代器简化集合接口(Iterators simplify the Aggregate interface. Iterator’s traversal interface obviates the need for a similar interface in Aggregate)
  4. 多种遍历方式可以pending给一个集合。(More than one traversal can be pending on an aggregate)
文章目录
  1. 1. 简单介绍
  2. 2. UML图
  3. 3. 实例
  4. 4. 输出
  5. 5. 效果