组合模式的官方定义是:把对象组合成一个树形结构来表示局部-整体的结构。组合模式让客户端可以统一地处理单独对象与对象组合。

英文定义如下:

Compose objects into tree structures to representpart-whole hierarchies. Composite lets clients treat individual objects and compositions of objectsuniformly.

定义中有三个比较重要的关键词(红色粗提部分):

第一个是tree structures,组合模式给我的感觉其实就是一颗树,能够使用到组合模式的问题,其数据结构都能抽象成一颗树,如果对于数据结构理解比较好的话,本章应该是比较简单的。

第二个是part-whole,看到part-whole,我一下联想到了本科学习图形学的时候接触到一点分形图形学的知识,因此这个理解起来也很简单,也就是局部自相似性。

第三个是uniformly,正式因为组合模式的结构是一个具有局部字相似性的数形结构,因此就可以统一地处理单个对象(类似与树中的叶子节点)与对象组合(类似与树中的父节点)。


实现组合模式的一个关键点就是,定义了一个即可以表示单个简单节点,又可以表示节点容器的基类。

The key to the composite pattern is an abstract class that represents both primitives and containers.

这样基类的必然会定义一些冗余操作,并且会带来不安全操作的副作用,所以违反了OO设计的一个原则:一个类只能定义对子类有意义的操作。

A class should only define operations that are meaningful to its subclasses.

当然这是进行OO设计不可避免的trade-off,鱼和熊掌不可兼得,关键取决于我们更需要什么,因为组合模型的一个特点就是uniformly,所以transparency比safety重要。


组合模式的使用范围是非常广泛的,最简单的例子就是MVC模型中的View,就用到了组合模式。


下面以代码为例,说明组合模式的实现方法。

首先定义一个组合模式中用到的那个比较重要的基类

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 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.println(" " + getName());
if (isVegetarian()) {
System.out.print("(v)");
}

System.out.println(", " + getPrice());
System.out.println(" -- " + getDescription());
}
}


再定义组合父节点类,一个包含叶子节点的容器。

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.println("\n" + getName());
System.out.println(", " + getDescription());
System.out.println("---------------------");

Iterator iterator = menuComponents.iterator();
while (iterator.hasNext()) {
MenuComponent menuComponent =
(MenuComponent)iterator.next();
menuComponent.print();
}
}
}


客户端代码如下:

public class Waitress {
MenuComponent allMenus;

public Waitress(MenuComponent allMenus) {
this.allMenus = allMenus;
}

public void printMenu() {
allMenus.print();
}
}


最后,测试代码

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");

allMenus.add(pancakeHouseMenu);
allMenus.add(dinerMenu);
allMenus.add(cafeMenu);

// add menu items here

dinerMenu.add(new MenuItem(
"Pasta",
"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
true,
3.89));
dinerMenu.add(dessertMenu);

dessertMenu.add(new MenuItem(
"Apple Pie",
"Apple pie with a flakey crust, topped with vanilla icecream",
true,
1.59));

// add more menu items here

Waitress waitress = new Waitress(allMenus);

waitress.printMenu();
}
}


至此,组合模式的使用就介绍完了。


可以看出,组合模式是一个数据抽象的结构,这个模式的使用应该比较简单,因为树形结构的数据可以很容易地看出。