组合模式
允许你将对象组合成树形结构来表现“整体/部分”层析结构。组合能让客户以一致的方式处理个别对象以及对象组合。
我们以菜单为例思考这一切:这个模式能够创建一个树形结构,在同一个结构中处理嵌套菜单和菜单项组。通过将菜单和项放在相同的结构中,我们创建了一个“整体/部分”层次结构,即由菜单和菜单项组成的对象树。但是可以将它视为一个整体,像是一个丰富的大菜单。一旦有了丰富的大菜单,我们就可以使用这个模式来“统一处理个别对象和组合对象”。这意味着,如果我们有了一个树形结构的菜单、子菜单和可能还带有菜单项的子菜单,那么任何一个菜单都是一种“组合”。因为它既可以包含其他菜单,也可以包含菜单项。个别对象只是菜单项——并未持有其他对象。
具体例子
组件类:MenuComponent.java
package com.designpattern.composite_iterator;
import java.util.Iterator;
public abstract class MenuComponent {
public void add(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 void print(){
throw new UnsupportedOperationException();
}
public Iterator<?> createIterator(){
throw new UnsupportedOperationException();
}
}
菜单项:MenuItem.java
package com.designpattern.composite_iterator;
import java.util.Iterator;
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 void print() {
System.out.print("2 " + getName());
if(isVegetarian()){
System.out.print("(v)");
}
System.out.println(", " + getPrice());
System.out.println(" -- " + getDescription());
}
//外部迭代器;
public Iterator<?> createIterator() {
return new NullIterator();
}
}
菜单:Menu.java
package com.designpattern.composite_iterator;
import java.util.ArrayList;
import java.util.Iterator;
public class Menu extends 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 void print() {
System.out.print("1\n" + getName());
System.out.println(", " + getDescription());
System.out.println("-------------------");
/**
* 内部迭代器
*
* 用它遍历所有菜单组件,可能是菜单项,也有可能是菜单;
* 注意:如果遇到另一个菜单对象,它的print()方法会开始另一个遍历,以此类推;
*
* */
Iterator<?> iterator = menuComponents.iterator();
while (iterator.hasNext()) {
MenuComponent menuComponent = (MenuComponent)iterator.next();
menuComponent.print();
}
}
//外部迭代器
public Iterator<?> createIterator() {
return new CompositeIterator(menuComponents.iterator());
}
}
菜单项迭代器:NullIterator.java
package com.designpattern.composite_iterator;
import java.util.Iterator;
public class NullIterator implements Iterator<Object>{
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
@Override
public Object next() {
// TODO Auto-generated method stub
return null;
}
@Override
public void remove() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException();
}
}
菜单迭代器:CompositeIterator.java
package com.designpattern.composite_iterator;
import java.util.Iterator;
import java.util.Stack;
public class CompositeIterator implements Iterator<Object>{
Stack<Object> stack = new Stack<Object>();
public CompositeIterator(Iterator<?> iterator){
stack.push(iterator);
}
public boolean hasNext() {
if(stack.empty()){
return false;
}else{
Iterator<?> iterator = (Iterator<?>) stack.peek();
if(!iterator.hasNext()){
stack.pop();
return hasNext();
} else{
return true;
}
}
}
public Object next() {
if(hasNext()){
Iterator<?> iterator = (Iterator<?>) stack.peek();
MenuComponent component = (MenuComponent) iterator.next();
if(component instanceof Menu){
stack.push(component.createIterator());
}
return component;
}else{
return null;
}
}
public void remove() {
throw new UnsupportedOperationException();
}
}
操作者:Waitress.java
package com.designpattern.composite_iterator;
import java.util.Iterator;
/**
* 操作菜单的类
* */
public class Waitress {
MenuComponent allMenus;
public Waitress(MenuComponent allMenus){
this.allMenus = allMenus;
}
public void printMenu(){
allMenus.print();
}
//使用外部迭代器
public void printVegetarianMenu(){
Iterator<?> iterator = allMenus.createIterator();
System.out.println("\nVEGETARIAN MENU\n----");
while(iterator.hasNext()){
MenuComponent menuComponent = (MenuComponent)iterator.next();
try {
if(menuComponent.isVegetarian()){
menuComponent.print();
}
} catch (UnsupportedOperationException e) {
}
}
}
}
测试类:Test.java
package com.designpattern.composite_iterator;
public class Test {
public static void main(String[] args) {
MenuComponent pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU", "Breakfast");
MenuComponent dinerMenu = new Menu("DINER MENU", "Lunch");
dinerMenu.add(new MenuItem("Pasta", "Spaghetti with Marinara Sauce, and a slice of sourdough bread", true, 3.89));
MenuComponent dessertMenu = new Menu("DESSERT MENU", "Dessert of course!");
dessertMenu.add(new MenuItem("Apple Pie", "Apple pie with a flakey crust, topped with vanilla ice cream", true, 1.59));
dinerMenu.add(dessertMenu);
MenuComponent cafeMenu = new Menu("CAFE MENU", "Dinner");
MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined");
allMenus.add(pancakeHouseMenu);
allMenus.add(dinerMenu);
allMenus.add(cafeMenu);
Waitress waitress = new Waitress(allMenus);
waitress.printMenu();
waitress.printVegetarianMenu();
}
}
总结
在写MenuComponent类的print()方法时,我们利用了一个迭代器来遍历组件内的每个项。如果遇到的是菜单(而不是菜单项),我们就会递归地调用print()方法处理它。换句话说,MenuComponent是在“内部”自行处理遍历。
但在Waitress类中,我们实现的是一个“外部”迭代器,所以有许多需要追踪的事情。外部迭代器必须维护它在遍历中的位置,以便外部客户可以通过调用hasNext()和next()来驱动遍历。在这个例子中,我们的代码也必须维护组合递归结构的位置。这也就是为什么当我们在组合层次结构中上上下下时,使用堆栈来维护我们的位置。
但是存在一个问题:使用外部迭代器时有的数据会打印多遍,取决于层级结构,这个貌似是迭代器本身的问题,想只打印一遍数据,需另作处理,或者直接用“内部”迭代器的方式。