迭代器模式(Iterator)要解決的問題:聚類的遍歷和處理
迭代器模式(Iterator)定義:
提供一種方法順序訪問一個聚合對象中的各個元素晴竞,而又不暴露該對象的內(nèi)部表示(內(nèi)部數(shù)據(jù)結(jié)構(gòu))
例子:菜單合并陈瘦,第一個菜單采用數(shù)組,第二個菜單采用List,第三個菜單采用(key,value),現(xiàn)在服務員需要打印所有菜單上的菜品
解決:讓這三個菜單提供一個接口對象(該接口提供訪問菜單上的菜品的方法)夯巷,使用該對象即可訪問菜品
數(shù)組需要實現(xiàn)Iterator接口偶妖,List已經(jīng)實現(xiàn)夏醉,map.values().iterator()
參考例子-極客學院
目錄結(jié)構(gòu):
FirstMenu:
public class FirstMenu {
private MenuItem[] menu = new MenuItem[3];
private int i = 0;
public FirstMenu() {
this.addMenuItem(new MenuItem("西瓜", "我是西瓜", 1));
this.addMenuItem(new MenuItem("哈密瓜", "我是哈密西瓜", 2));
this.addMenuItem(new MenuItem("角瓜", "我是角瓜", 3));
}
public MenuItem[] getMenu() {
return menu;
}
public void addMenuItem(MenuItem item) {
menu[i] = item;
i++;
}
public Iterator getIterator() {
return new FistMenuIterator();
}
class FistMenuIterator implements Iterator {
private int position = 0;
@Override
public boolean hasNext() {
return position < FirstMenu.this.i;
}
@Override
public Object next() {
MenuItem item = FirstMenu.this.menu[this.position];
this.position++;
return item;
}
@Override
public void remove() {
}
}
}
SecondMenu:
public class SecionMenu {
private ArrayList<MenuItem> list = new ArrayList<MenuItem>();
public SecionMenu() {
this.list.add(new MenuItem("土豆", "tudobu", 4));
this.list.add(new MenuItem("茄子", "213124", 5));
this.list.add(new MenuItem("豆角", "jhgkuyi", 6));
}
public Iterator getIterator() {
return this.list.iterator();
}
}
ThirdMenu:
public class ThirdMenu {
private Map<String, MenuItem> map = new HashMap<String, MenuItem>();
public ThirdMenu() {
map.put("1", new MenuItem("牛肉", "牛肉餡餃子", 20));
map.put("2", new MenuItem("羊肉", "羊肉餡餃子", 30));
map.put("3", new MenuItem("豬肉", "豬肉餡餃子", 10));
}
public Iterator getIterator() {
return this.map.values().iterator();
}
}
菜品實體:MenuItem
public class MenuItem {
private String name;
private String desc;
private int price;
public MenuItem(String name, String desc, int price) {
this.name = name;
this.desc = desc;
this.price = price;
}
public String getName() {
return name;
}
public String getDesc() {
return desc;
}
public int getPrice() {
return price;
}
}
服務員Waitress持有訪問菜單聚合實體中每個對象的接口(Iterator)的集合
public class Waitress {
private List<Iterator> list;
public Waitress() {
this.list = new ArrayList<Iterator>();
}
public void addMenu(Iterator menu) {
this.list.add(menu);
}
public void printAllMenu() {
for (Iterator iterator : list) {
System.out.println("-----------------------------");
while (iterator.hasNext()) {
MenuItem item = (MenuItem) iterator.next();
System.out.println(item.getName() + ":" + item.getDesc() + ":" + item.getPrice());
}
System.out.println("-----------------------------");
}
}
}
測試類Main:
public class Main {
public static void main(String[] args) {
FirstMenu firstMenu = new FirstMenu();
SecionMenu secionMenu = new SecionMenu();
ThirdMenu thirdMenu = new ThirdMenu();
Waitress waitress = new Waitress();
waitress.addMenu(firstMenu.getIterator());
waitress.addMenu(secionMenu.getIterator());
waitress.addMenu(thirdMenu.getIterator());
waitress.printAllMenu();
}
}
輸出結(jié)果:
總結(jié):** 面向接口編程术裸,接口中提供訪問對象的方法(對對象的一系列動作)额嘿,想對對象做某些更改或者查詢時直接向外暴露接口即可本昏。 **