一:相关定义
组合模式:允许你将对象组合成树形结构来表现“整体/部分”层次结构。组合能让客户以一致的方式处理个别对象以及对象组合。
二:相关类图的示例
组合模式类图:
使用组合结构,我们能把相同的操作应用在组合和个别对象上。换句话说,大多数情况下,我们可以忽略对象组合和个别对象之间的差距。
组合模式示例:
这是一个菜单的组合模式示例,在一个菜单中,可能包含有一个子菜单,也可能包含一个菜品;使用组合模式后,waitress在遍历菜单时不用关心菜单中是子菜单还是菜品,只需要使用统一的接口就可以了,大大便利了客户端的操作。下面是具体代码:
菜单组件MenuComponent
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 String getPrice(){
throw new UnsupportedOperationException();
}
public boolean isVegetarian(){
throw new UnsupportedOperationException();
}
public void print(){
throw new UnsupportedOperationException();
}
}
菜单组件包含了菜品和子菜单的所有方法,这里默认实现抛出异常,叶子节点和组合节点都需要继承菜单组件。
菜单项MenuItem:
public class MenuItem extends MenuComponent{
String name;
String description;
boolean vegetarain;
double price;
public MenuItem(String name,
String description,
boolean vegetarian,
double price){
this.name = name;
this.description = description;
this.vegetarian = vegatarian;
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(" "+getName() );
if(isVegetarian()){
System.out.print("(V)");
}
System.out.print(", "+getPrice());
System.out.print(" -- "+getDescription());
}
}
组合菜单:
public class Menu extends MenuComponent{
ArrayList menuComponents = new ArrayList();
String name;
String description;
public Menu(ArrayList menuComponents, String name, String description) {
this.menuComponents = menuComponents;
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 it = menuComponents.iterator();
while (it.hasNext()){
MenuComponent menuComponent = (MenuComponent) it.next();
menuComponent.print();
}
}
}
客户端waitress:
public class Waitress{
MenuComponent allMenus;
public Waitress(MenuComponent allMenus){
this.allMenus = allMenus;
}
public void printMenu(){
allMenus.print();
}
}
这里,主要的代码就完成了。女招待只要调用顶层的print()方法,就可以完成对整个菜单的遍历了。
参考书籍:《head first 设计模式》