组合模式:允许你将对象组合成树形结构来表现“整体/部分”层次结构。组合能让客户以一致的方式处理个别对象以及对象组合。当你有数个对象的集合,他们彼此有“”整体/部分”的关系,并且你想要一致的方式对待这些对象时,就可以使用这种模式。常见的树形结构,父节点和叶子节点都可能有名称,描述等字段,但他们的实现方法却不一样。
public class MenuComponent {
public void add(MenuComponent menuComponent) {
}
public void remove(MenuComponent menuComponent) {
}
public MenuComponent getChild(int i) {
return null;
}
public String getName() {
return null;
}
public String getDescription() {
return null;
}
public double getPrice() {
return 0.0;
}
public boolean isVegetarian() {
return false;
}
public void print() {
}
}
public class Menu extends MenuComponent{
List<MenuComponent> menuComponents = new ArrayList<>();
String name;
String description;
public Menu(String name, String description) {
super();
this.name = name;
this.description = description;
}
@Override
public void add(MenuComponent menuComponent) {
// TODO Auto-generated method stub
menuComponents.add(menuComponent);
}
@Override
public void remove(MenuComponent menuComponent) {
// TODO Auto-generated method stub
menuComponents.remove(menuComponent);
}
@Override
public MenuComponent getChild(int i) {
// TODO Auto-generated method stub
return menuComponents.get(i);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public void print() {
System.out.print("\n"+getName());
System.out.println(", "+getDescription());
System.out.println("---------------");
Iterator<MenuComponent> iterator = menuComponents.iterator();
while(iterator.hasNext()) {
MenuComponent meComponent = iterator.next();
meComponent.print();
}
}
}
public class MenuItem extends MenuComponent{
String name;
String description;
boolean vegetarian;
double price;
public MenuItem(String name, String description, boolean vegetarian, double price) {
super();
this.name = name;
this.description = description;
this.vegetarian = vegetarian;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isVegetarian() {
return vegetarian;
}
public void setVegetarian(boolean vegetarian) {
this.vegetarian = vegetarian;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public void print() {
System.out.print(" "+getName());
if(isVegetarian()) {
System.out.print("(V)");
}
System.out.println(", "+getPrice());
System.out.println(" --"+getDescription());
}
}
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);
dinerMenu.add(new MenuItem("Pasta", "Spaghetti with Marinara Sauce", true, 3.89));
dinerMenu.add(dessertMenu);
dinerMenu.add(new MenuItem("Appie pie","Appie pie",true,6));
allMenus.print();
}
}