迭代器模式是与集合共生共死的,一般来说,我们只要实现一个集合,就需要同时提供这个集合的迭代器,就像java中的Collection,List、Set、Map等,这些集合都有自己的迭代器。假如我们要实现一个这样的新的容器,当然也需要引入迭代器模式,给我们的容器实现一个迭代器。————题记
设计模式
迭代器模式:提供一种方法顺序访问一个聚合对象中的各个元素,而不暴露其内部的表示。
把游走的任务放在迭代器上,而不是聚合上。这样简化了聚合的接口和实现,也让责任各得其所。
组合模式:允许你将对象组合成树形结构来表现“整体/部分”分层结构。组合能让客户以一致的方式处理个别对象以及对象组合。
使用组合结构,我们能把相同的操作应用在组合和个别对象上。换句话说,在大多数情况下,我们可以忽略对象组合和个别对象之间的差别。
设计原则
单一责任:一个类应该只有一个引起变化的原因。
要点
迭代器允许访问聚合的元素,而不需要暴露他的内部结构。
迭代器将遍历聚合的工作封装到一个对象中。
迭代器提供了一个通用的接口,让我们遍历聚合的项,当我们编码使用聚合的项时,就可以使用多态机制。
组合模式允许客户对个别对象和组合对象一视同仁。
在实现组合模式时,有许多设计上的折中,你要根据需要平衡透明性和安全性。
模型匹配
策略模型 封装可互换的相位,并使用委托决定使用哪一个
适配器模型 改变一个或多个类的接口
迭代器模型 提供一个方式来遍历集合,而无需暴露集合的实现
外观模型 简化一群类的接口
组合模型 客户可以将对象的集合以及个别对象一视同仁
观察者模型 当某个状态改变时,允许一群对象能被通知到
迭代器模式:
//定义迭代器接口
public interface Iterator {
boolean hasNext();
Object next();
}
// implements 实现具体接口
public class DinerMenuIterator implements Iterator {
MenuItem[] items;
int position = 0;
public DinerMenuIterator(MenuItem[] items) {
this.items = items;
}
public Object next() {
MenuItem menuItem = items[position];
position = position + 1;
return menuItem;
}
public boolean hasNext() {
if (position >= items.length || items[position] == null) {
return false;
} else {
return true;
}
}
}
public class DinerMenu implements Menu {
static final int MAX_ITEMS = 6;
int numberOfItems = 0;
MenuItem[] menuItems;
public DinerMenu() {
menuItems = new MenuItem[MAX_ITEMS];
addItem("Vegetarian BLT",
"(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99);
addItem("BLT",
"Bacon with lettuce & tomato on whole wheat", false, 2.99);
addItem("Soup of the day",
"Soup of the day, with a side of potato salad", false, 3.29);
addItem("Hotdog",
"A hot dog, with saurkraut, relish, onions, topped with cheese",
false, 3.05);
addItem("Steamed Veggies and Brown Rice",
"Steamed vegetables over brown rice", true, 3.99);
addItem("Pasta",
"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
true, 3.89);
}
public void addItem(String name, String description,
boolean vegetarian, double price)
{
MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
if (numberOfItems >= MAX_ITEMS) {
System.err.println("Sorry, menu is full! Can't add item to menu");
} else {
menuItems[numberOfItems] = menuItem;
numberOfItems = numberOfItems + 1;
}
}
//返回迭代器接口
public Iterator createIterator() {
return new DinerMenuIterator(menuItems);
}
// other menu methods here
}
package net.dp.iterator.dinermerger;
public class Waitress {
PancakeHouseMenu pancakeHouseMenu;
DinerMenu dinerMenu;
//在构造器中,女招待照顾两个菜单
public Waitress(PancakeHouseMenu pancakeHouseMenu, DinerMenu dinerMenu) {
this.pancakeHouseMenu = pancakeHouseMenu;
this.dinerMenu = dinerMenu;
}
public void printMenu() {
//为每一个菜单各自创建一个迭代器
Iterator pancakeIterator = pancakeHouseMenu.createIterator();
Iterator dinerIterator = dinerMenu.createIterator();
System.out.println("MENU\n----\nBREAKFAST");
//对每一个迭代器调用重载printMenu(),将迭代器传入
printMenu(pancakeIterator);
System.out.println("\nLUNCH");
printMenu(dinerIterator);
}
private void printMenu(Iterator iterator) {
while (iterator.hasNext()) {
MenuItem menuItem = (MenuItem)iterator.next();
System.out.print(menuItem.getName() + ", ");
System.out.print(menuItem.getPrice() + " -- ");
System.out.println(menuItem.getDescription());
}
}
// other methods here
}
组合模式:
//MenuComponent对每个方法都提供了默认的实现
public abstract class MenuComponent {
public void add(MenuComponent menuComponent) {
//所有的组件都必须实现MenuComponent接口,然而叶节点和组合节点的角色不同,所以有些方法可能并不适合某种节点。面对这种情况,有时候你最好抛出运行时异常。
throw new UnsupportedOperationException();
}
public void remove(MenuComponent menuComponent) {
throw new UnsupportedOperationException();
}
public MenuComponent getChild(int i) {
throw new UnsupportedOperationException();
}
public String getName() {
throw new UnsupportedOperationException();
}
public String getDescription() {
throw new UnsupportedOperationException();
}
public double getPrice() {
throw new UnsupportedOperationException();
}
public boolean isVegetarian() {
throw new UnsupportedOperationException();
}
public abstract Iterator<MenuComponent> createIterator();
public void print() {
throw new UnsupportedOperationException();
}
}
//首先扩展MenuComponent接口,实现菜单项
public class MenuItem extends MenuComponent {
String name;
String description;
boolean vegetarian;
double price;
public MenuItem(String name, String description, boolean vegetarian,
double price) {
this.name = name;
this.description = description;
this.vegetarian = vegetarian;
this.price = price;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public double getPrice() {
return price;
}
public boolean isVegetarian() {
return vegetarian;
}
public Iterator<MenuComponent> createIterator() {
return new NullIterator();
}
//对于菜单项来说,此方法会打印出完整的菜单项条目
public void print() {
System.out.print(" " + getName());
if (isVegetarian()) {
System.out.print("(v)");
}
System.out.println(", " + getPrice());
System.out.println(" -- " + getDescription());
}
// vv MenuItemCompositeV2Main
}
//实现组合菜单
public class Menu extends MenuComponent {
//菜单可以有任意数目的孩子,这些孩子都必须属于MenuComponent类型
ArrayList<MenuComponent> menuComponents = new ArrayList<MenuComponent>();
String name;
String description;
public Menu(String name, String description) {
this.name = name;
this.description = description;
}
public void add(MenuComponent menuComponent) {
menuComponents.add(menuComponent);
}
public void remove(MenuComponent menuComponent) {
menuComponents.remove(menuComponent);
}
public MenuComponent getChild(int i) {
return (MenuComponent)menuComponents.get(i);
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public Iterator<MenuComponent> createIterator() {
return new CompositeIterator(menuComponents.iterator());
}
public void print() {
System.out.print("\n" + getName());
System.out.println(", " + getDescription());
System.out.println("---------------------");
//使用迭代器,遍历所有组件
Iterator<MenuComponent> iterator = menuComponents.iterator();
while (iterator.hasNext()) {
MenuComponent menuComponent =
iterator.next();
menuComponent.print();
}
}
}
public class Waitress {
MenuComponent allMenus;
//只需要将最顶层菜单交给招待就行
public Waitress(MenuComponent allMenus) {
this.allMenus = allMenus;
}
public void printMenu() {
allMenus.print();
}
public void printVegetarianMenu() {
Iterator<MenuComponent> iterator = allMenus.createIterator();
System.out.println("\nVEGETARIAN MENU\n----");
while (iterator.hasNext()) {
MenuComponent menuComponent = iterator.next();
try {
//我们调用全部的menuComponent的isVegetarian方法,但是Menu会抛出一个异常,因为他们不支持这个操作
if (menuComponent.isVegetarian()) {
menuComponent.print();
}
} catch (UnsupportedOperationException e) {
//如果菜单组件不支持这个操作,我们就对这个异常置之不理。
}
}
}
}
package net.dp.composite.menuiterator;
public class MenuTestDrive {
public static void main(String args[]) {
//创建所有菜单
MenuComponent pancakeHouseMenu =
new Menu("PANCAKE HOUSE MENU", "Breakfast");
MenuComponent dinerMenu =
new Menu("DINER MENU", "Lunch");
MenuComponent cafeMenu =
new Menu("CAFE MENU", "Dinner");
MenuComponent dessertMenu =
new Menu("DESSERT MENU", "Dessert of course!");
MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined");
//使用组合的add方法,将每个菜单加入到顶层菜单中
allMenus.add(pancakeHouseMenu);
allMenus.add(dinerMenu);
allMenus.add(cafeMenu);
//加入各个菜单项
pancakeHouseMenu.add(new MenuItem(
"K&B's Pancake Breakfast",
"Pancakes with scrambled eggs, and toast",
true,
2.99));
//加入更多的菜单项
//一旦将整个财大层次构造万别,将它交给女招待
Waitress waitress = new Waitress(allMenus);
waitress.printVegetarianMenu();
}
}