定義
提供一種方法順序訪問一個容器對象中的各個元素,而又不需要暴露該對象的內(nèi)部表示。
Android 源碼中使用迭代器模式
Cursor
特點
優(yōu)點
(1)符合面向?qū)ο笤O(shè)計原則中的單一職責(zé)原則。
(2)支持對容器對象的多種遍歷。弱化了容器類與遍歷算法之間的關(guān)系。缺點
(1)類文件的增加。
(3)會出現(xiàn)ConcurrentModificationException異常。
(2)遍歷過程是一個單向且不可逆的遍歷。
使用場景
遍歷一個容器對象時。
簡單實現(xiàn)
- 實體類
/**
* 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 + '\'' +
'}';
}
}
- 迭代器接口
/**
* 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;
}
}
- 容器接口
/**
* 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,"男","產(chǎn)品"));
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,"女","產(chǎn)品"));
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);
}
}
- 使用
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());
}
}
}
- 結(jié)果
2019-04-12 11:26:12 D/TAG: Employee{name='小民', age=23, sex='男', position='產(chǎn)品'}
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='產(chǎn)品'}
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'}