Android开发项目踩坑Android进阶

解决问题的“第三者”——迭代器模式

2019-03-14  本文已影响25人  程序员丶星霖

《Android源码设计模式解析与实战》读书笔记(十四)
《Android源码设计模式解析与实战》PDF资料下载

一、迭代器模式简介

迭代器模式又称为游标模式,是行为型设计模式之一。

1.1、定义

提供一宗方法顺序访问一个容器对象中的各个元素,而又不需要暴露该对象的内部表示。

1.2、使用场景

遍历一个容器对象时。

二、迭代器模式的简单实现

public class Employee {
    private String name;//姓名
    private int age;//年龄
    private String sex;//性别
    private String position;//职位

    public Employee(String name, int age, String sex, String position) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.position = position;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", position='" + position + '\'' +
                '}';
    }
}
public class CompanyMin {
    private List<Employee> list = new ArrayList<>();

    public CompanyMin(){
        list.add(new Employee("小民", 96, "男", "程序猿"));
        list.add(new Employee("小芸", 22, "女", "测试"));
        list.add(new Employee("小芳", 18, "女", "测试"));
        list.add(new Employee("可儿", 21, "女", "设计"));
        list.add(new Employee("朗情", 19, "女", "设计"));
    }

    public List<Employee> getList() {
        return list;
    }
}
public class CompanyHui {
    private Employee[] array = new Employee[3];

    public CompanyHui() {
        array[0] = new Employee("辉哥", 108, "男", "程序员");
        array[1] = new Employee("小红", 98, "男", "程序员");
        array[2] = new Employee("小辉", 88, "男", "程序员");
    }

    public Employee[] getArray() {
        return array;
    }
}
public class Boss {
    public static void main(String[] args) {
        CompanyMin min = new CompanyMin();
        List minList = min.getList();
        for (int i = 0; i < minList.size(); i++) {
            System.out.println(minList.get(i).toString());
        }

        CompanyHui hui = new CompanyHui();
        Employee[] huiArray = hui.getArray();
        for (int i = 0; i < huiArray.length; i++) {
            System.out.println(huiArray[i]);
        }
    }
}

输出结果:

迭代器模式.png

备忘录模式优化:

public interface Iterator {
    /**
     * 是否还有下一个元素
     */
    boolean hasNext();

    /**
     * 返回当前位置的元素,并将位置移至下一位
     */
    Object next();
}
public class MinIterator implements Iterator {
    private List<Employee> list;
    private int position;

    public MinIterator(List<Employee> list) {
        this.list = list;
    }

    @Override
    public boolean hasNext() {
        return !(position > list.size() - 1 || list.get(position) == null);
    }

    @Override
    public Object next() {
        Employee employee = list.get(position);
        position++;
        return employee;
    }
}
public class HuiIterator implements Iterator {
    private Employee[] array;
    private int position;

    public HuiIterator(Employee[] array) {
        this.array = array;
    }

    @Override
    public boolean hasNext() {
        return !(position>array.length-1||array[position]==null);
    }

    @Override
    public Object next() {
        Employee employee = array[position];
        position++;
        return employee;
    }
}
public interface Company {
    /**
     * 返回一个迭代器对象
     */
    Iterator iterator();
}
public class CompanyMin implements Company {
    private List<Employee> list = new ArrayList<>();

    public CompanyMin() {
        list.add(new Employee("小民", 96, "男", "程序猿"));
        list.add(new Employee("小芸", 22, "女", "测试"));
        list.add(new Employee("小芳", 18, "女", "测试"));
        list.add(new Employee("可儿", 21, "女", "设计"));
        list.add(new Employee("朗情", 19, "女", "设计"));
    }

    public List<Employee> getList() {
        return list;
    }

    @Override
    public Iterator iterator() {
        return new MinIterator(list);
    }
}
public class CompanyHui implements Company {
    private Employee[] array = new Employee[3];

    public CompanyHui() {
        array[0] = new Employee("辉哥", 108, "男", "程序员");
        array[1] = new Employee("小红", 98, "男", "程序员");
        array[2] = new Employee("小辉", 88, "男", "程序员");
    }

    public Employee[] getArray() {
        return array;
    }

    @Override
    public Iterator iterator() {
        return new HuiIterator(array);
    }
}
public class Boss {
    public static void main(String[] args) {
        CompanyMin min = new CompanyMin();
        check(min.iterator());

        CompanyHui hui = new CompanyHui();
        check(hui.iterator());
    }

    private static void check(Iterator iterator) {
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toString());
        }
    }
}

输出结果:

迭代器模式.png

三、总结

对于迭代器模式来说,其自身优点很明显也很单一,支持以不同的方式去遍历一个容器对象,也可以有多个遍历,弱化了容器类与遍历算法之间的关系,而缺点就是对类文件的增加。

学海无涯苦作舟

我的微信公众号
上一篇 下一篇

猜你喜欢

热点阅读