美文网首页
设计模式之迭代器模式

设计模式之迭代器模式

作者: Kevin_小飞象 | 来源:发表于2019-04-12 11:39 被阅读0次

定义

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

Android 源码中使用迭代器模式

Cursor

特点

  • 优点
    (1)符合面向对象设计原则中的单一职责原则。
    (2)支持对容器对象的多种遍历。弱化了容器类与遍历算法之间的关系。

  • 缺点
    (1)类文件的增加。
    (3)会出现ConcurrentModificationException异常。
    (2)遍历过程是一个单向且不可逆的遍历。

使用场景

遍历一个容器对象时。

简单实现

  1. 实体类
/**
 * Created on 2019/4/12 10:45
 *
 * @author Scarf Gong
 */
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 + '\'' +
                '}';
    }
}
  1. 迭代器接口
/**
 * Created on 2019/4/12 10:43
 * 迭代器接口
 * @author Scarf Gong
 */
public interface Iterator {
    boolean hasNext();  //是否还有下一个

    Object next();    //位置移到下一个
}

--------------------------------
/**
 * Created on 2019/4/12 10:48
 *  小民迭代器
 * @author Scarf Gong
 */
public class MinIterator implements Iterator {
    private List<Employee> mList;

    private int position;

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

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

    @Override
    public Object next() {
        Employee employee = mList.get(position);
        position++;
        return employee;
    }
}

--------------------------
/**
 * Created on 2019/4/12 10:48
 * 小琴迭代器
 * @author Scarf Gong
 */
public class QinIterator implements Iterator {
    private List<Employee> mList;

    private int position;

    public QinIterator(List<Employee> list) {
        mList = list;
    }

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

    @Override
    public Object next() {
        Employee employee = mList.get(position);
        position++;
        return employee;
    }
}

  1. 容器接口
/**
 * Created on 2019/4/12 10:53
 *
 * @author Scarf Gong
 */
public interface Company {
    Iterator iterator();
}

----------------------
/**
 * Created on 2019/4/12 10:54
 *
 * @author Scarf 
 */
public class CompanyMin implements Company {
    private List<Employee> mList = new ArrayList<>();

    public CompanyMin() {
        mList.add(new Employee("小民",23,"男","产品"));
        mList.add(new Employee("小李",20,"女","测试"));
        mList.add(new Employee("小包",22,"男","测试"));
        mList.add(new Employee("小惠",18,"女","UI"));
        mList.add(new Employee("小图",22,"女","UI"));
    }

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

    @Override
    public Iterator iterator() {
        return new MinIterator(mList);
    }
}
--------------------------
/**
 * Created on 2019/4/12 10:54
 *
 * @author Scarf Gong
 */
public class CompanyQin implements Company {
    private List<Employee> mList = new ArrayList<>();

    public CompanyQin() {
        mList.add(new Employee("小琴",23,"女","产品"));
        mList.add(new Employee("小汪",20,"男","Android"));
        mList.add(new Employee("小包",22,"男","iOS"));
    }

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

    @Override
    public Iterator iterator() {
        return new MinIterator(mList);
    }
}

  1. 使用
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
    }

    private void initData() {
        CompanyMin min = new CompanyMin();
        check(min.iterator());

        Log.d("TAG", "------------------");

        CompanyQin qin = new CompanyQin();
        check(qin.iterator());

    }

    public void check(Iterator iterator) {
        while (iterator.hasNext()) {
            Log.d("TAG", iterator.next().toString());
        }
    }

}
  1. 结果
2019-04-12 11:26:12 D/TAG: Employee{name='小民', age=23, sex='男', position='产品'}
2019-04-12 11:26:12 D/TAG: Employee{name='小李', age=20, sex='女', position='测试'}
2019-04-12 11:26:12 D/TAG: Employee{name='小包', age=22, sex='男', position='测试'}
2019-04-12 11:26:12 D/TAG: Employee{name='小惠', age=18, sex='女', position='UI'}
2019-04-12 11:26:12 D/TAG: Employee{name='小图', age=22, sex='女', position='UI'}
2019-04-12 11:26:12 D/TAG: ------------------
2019-04-12 11:26:12 D/TAG: Employee{name='小琴', age=23, sex='女', position='产品'}
2019-04-12 11:26:12 D/TAG: Employee{name='小汪', age=20, sex='男', position='Android'}
2019-04-12 11:26:12 D/TAG: Employee{name='小包', age=22, sex='男', position='iOS'}

相关文章

网友评论

      本文标题:设计模式之迭代器模式

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