设计模式——迭代器模式15

迭代器模式提供一种方法访问一个容器对象中各个元素,而又不需暴露该对象的内部细节。
设计模式,一定要敲代码理解
在这里插入图片描述

抽象迭代器

/**
 * 迭代抽象
 * */
public interface Iterator<A> {
    A next();
    boolean hasNext();
}

迭代器实现

/**
 * @author ggbond
 * @date 2024年04月13日 09:08
 */
public class MyIterator<T> implements Iterator<T>{
    private MyCollection<T> myCollection;
    private  int size;
    private int index; //索引游标

    public MyIterator(MyCollection<T> myCollection) {
        this.myCollection = myCollection;
        this.index = 0;
        this.size = myCollection.getSize();
    }

    @Override
    public T next() {
        if (index < size) {
            return myCollection.getNext(index++);
        }
        return null;

    }

    @Override
    public boolean hasNext() {
        return index < size;
    }
}

抽象集合

/**
 * 定义集合
 * */
public interface Collection<A> {
    boolean add(A a);
    boolean remove(A a);
    Iterator<A> createIterator();

}

集合实现

/**
 * @author ggbond
 * @date 2024年04月13日 09:12
 */
public class MyCollection<T> implements Collection<T>{
    private ArrayList<T> list=new ArrayList<T>();
    public int getSize() {
        return list.size();
    }
    @Override
    public boolean add(T t) {
       return list.add(t);
    }

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

    @Override
    public Iterator createIterator() {
        return new MyIterator(this);
    }

    public T getNext(int i) {
        return  list.get(i);
    }
}

迭代对象

/**
 * @author ggbond
 * @date 2024年04月13日 09:29
 */
public class Person {
    String name;
    String cardID;

    public Person(String name, String cardID) {
        this.name = name;
        this.cardID = cardID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCardID() {
        return cardID;
    }

    public void setCardID(String cardID) {
        this.cardID = cardID;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", cardID='" + cardID + '\'' +
                '}';
    }
}

测试与结果

public class Main {
    public static void main(String[] args) {

        Person p1= new Person("ggbond1","001");
        Person p2= new Person("ggbond2","002");
        Person p3= new Person("ggbond3","003");
        Person p4= new Person("ggbond4","004");
        Collection<Person> collection=new MyCollection<>();
        collection.add(p1);
        collection.add(p2);
        collection.add(p3);
        collection.add(p4);
        Iterator<Person> iterator = collection.createIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
Person{name='ggbond1', cardID='001'}
Person{name='ggbond2', cardID='002'}
Person{name='ggbond3', cardID='003'}
Person{name='ggbond4', cardID='004'}

总结

迭代器模式将数据存储和数据遍历的职责进行分离。但针对不同结构的迭代对象,迭代方式需进行添加。

代码下载

代码下载

相关推荐

  1. 设计模式(15):模式

    2024-04-13 11:04:05       33 阅读
  2. GO设计模式——18模式(行为型)

    2024-04-13 11:04:05       65 阅读
  3. 设计模式17、iterator 模式

    2024-04-13 11:04:05       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-13 11:04:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-13 11:04:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-13 11:04:05       82 阅读
  4. Python语言-面向对象

    2024-04-13 11:04:05       91 阅读

热门阅读

  1. 选择成为一名程序员:兴趣与职业发展的交织

    2024-04-13 11:04:05       35 阅读
  2. maya模板导入动画

    2024-04-13 11:04:05       134 阅读
  3. 微服务learning

    2024-04-13 11:04:05       119 阅读
  4. 揭示API威胁的攻击趋势(下)

    2024-04-13 11:04:05       40 阅读
  5. C++生成随机数游戏

    2024-04-13 11:04:05       40 阅读
  6. centos 7 使用dnf报错

    2024-04-13 11:04:05       35 阅读
  7. SASS 目录结构

    2024-04-13 11:04:05       37 阅读
  8. 五种主流数据库:子查询

    2024-04-13 11:04:05       126 阅读