1 介紹
小白: 大哥派诬,我今天又被mother罵了。
Acey:為什么呀链沼,是不是你又做錯(cuò)什么事了默赂?
小白: (⊙﹏⊙)??,我上學(xué)以來第一次曠課被抓了括勺,you konw缆八,the first time。疾捍。奈辰。。乱豆。老師對著名單一個(gè)個(gè)點(diǎn)名奖恰。
Acey:活該??,曠課有風(fēng)險(xiǎn)宛裕,逃課需謹(jǐn)慎呀大兄弟瑟啃。老師點(diǎn)名用的可是迭代模式呢,就不要想著能逃過啦揩尸。
圖片來源:百度圖片
迭代模式:Iterator模式是行為模式的一種翰守,它把容器內(nèi)的對象交給外部類,使用iterator來對整個(gè)聚合按順序遍歷疲酌。
小白: 按順序(lll¬ω¬),看到這個(gè)了袁,我下次又不敢再逃課了朗恳。。载绿。
Acey: 撐死膽大的粥诫,餓死膽小的。哈哈崭庸。其實(shí)你可以再試試哦怀浆。還是來說說iterator模式吧谊囚,其中這個(gè)容器就是一個(gè)空的名單表,而容器內(nèi)的對象就相當(dāng)于名單表上的學(xué)生信息了执赡,外部類就是迭代器了镰踏,這樣容器內(nèi)就不會(huì)有任何的訪問對象的方法,將訪問對象的方法的具體實(shí)現(xiàn)放到迭代器中沙合, 這樣奠伪,老師點(diǎn)名就無需再知道名單為什么這么排,只需要按照上面的順序的點(diǎn)名就可以了首懈。
小白:,,???,,绊率。soga
2 實(shí)現(xiàn)
首先先來看看類圖
其中
Iterator:迭代器接口
ConcreteIterator:具體的迭代器實(shí)現(xiàn)類,根據(jù)自己的需要實(shí)現(xiàn)操作方法
Aggregate:容器接口
ConcreteAggregate:具體的容器實(shí)現(xiàn)類
實(shí)現(xiàn)
第一步:創(chuàng)建學(xué)生
Student.class
public class Student {
private String idCard;
private String name;
private String sex;
public Student(String idCard, String name, String sex) {
super();
this.idCard = idCard;
this.name = name;
this.sex = sex;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
第二步:創(chuàng)建具體的迭代器實(shí)現(xiàn)類
Iter.class
//獲取迭代器
class Iter implements Iterator{
private List<?> lists;
private Integer index = 0;
//將值傳入迭代器
public Iter(List<?> list){
this.lists = list;
}
//判斷是否有下一個(gè)元素
@Override
public boolean hasNext() {
if(index >= lists.size()){
return false;
}else{
return true;
}
}
//獲取當(dāng)前元素
@Override
public Object next() {
return lists.get(index++);
}
//移除當(dāng)前元素
@Override
public void remove() {
lists.remove(index--);
}
}
第三步:創(chuàng)建學(xué)生容器
StudentsList.class
public class StudentList {
//名單
private List<Student> students;
//創(chuàng)建名單
public StudentList(){
students = new ArrayList<>();
}
//往名單中添加學(xué)生
public void add(Student student){
students.add(student);
}
//獲取名單的迭代器
public Iterator iterator(){
return new Iter(students);
}
}
第四步:測試
MainClass.class
public class MainClass {
public static void main(String[] args) {
Student xiaobai = new Student("110","小白", "man");
Student dabai = new Student("112","大白", "man");
Student wuhua = new Student("115","無化", "woman");
Student wuchang = new Student("120","黑無常", "unknow");
StudentList studentList = new StudentList();
studentList.add(wuchang);
studentList.add(wuhua);
studentList.add(dabai);
studentList.add(xiaobai);
Iterator iterator = studentList.iterator();
System.out.println("--------------------點(diǎn)名啦---
-----------------------------");
while(iterator.hasNext()){
Student student = (Student) iterator.next();
System.out.println(student.getName() + "已到");
}
}
}
因?yàn)橹皇鞘褂昧艘粋€(gè)具體的容器類究履,所以就沒有創(chuàng)建容器接口滤否。如果有多個(gè)容器的話,可以創(chuàng)建一個(gè)容器接口最仑,將通用的方法轉(zhuǎn)移到接口中藐俺,具體的實(shí)現(xiàn)類只需要實(shí)現(xiàn)該容器接口然后實(shí)現(xiàn)對應(yīng)的方法。
這樣當(dāng)我們需要修改訪問方法時(shí)盯仪,我們只需要在具體的迭代器實(shí)現(xiàn)類中修改紊搪,增加新的迭代器實(shí)現(xiàn)類和聚合類都很方便。當(dāng)然全景,它的優(yōu)點(diǎn)也變成了缺點(diǎn)耀石,當(dāng)我們過多的添加類后,整個(gè)系統(tǒng)的復(fù)雜性也隨之變大爸黄。
喜歡的話戳一下喜歡唄滞伟。
有什么建議的話希望大家能在下方回復(fù)??
上一篇:《職責(zé)鏈模式 - 為什么你的學(xué)費(fèi)比別人貴?》
下一篇:《模板模式 - 納新納新》