组合模式:将对象聚合成树形结构来表现“整体/部分”的层次结构。 组合模式能让客户以一致的方式来处理个别对象以及对象组合。 也就是我们可以忽略对象组合与个体对象之间的差别
一、餐厅菜单项目
二、代码
1、超类MenuComponent
代码语言:javascript复制public abstract class MenuComponent {
public String getName() {
return "";
}
public String getDescription() {
return "";
}
public float getPrice() {
return 0;
}
public boolean isVegetable() {
return false;
}
public abstract void print();
public Iterator getIterator() {
return new NullIterator();
}
}
2、CakeHouseMenu
代码语言:javascript复制public class CakeHouseMenu extends MenuComponent {
private ArrayList<MenuComponent> menuItems;
public CakeHouseMenu() {
menuItems = new ArrayList<MenuComponent>();
addItem("KFC Cake Breakfast", "boiled eggs&toast&cabbage", true, 3.99f);
addItem("MDL Cake Breakfast", "fried eggs&toast", false, 3.59f);
addItem("Stawberry Cake", "fresh stawberry", true, 3.29f);
addItem("Regular Cake Breakfast", "toast&sausage", true, 2.59f);
}
private void addItem(String name, String description, boolean vegetable, float price) {
MenuItem menuItem = new MenuItem(name, description, vegetable, price);
menuItems.add(menuItem);
}
public Iterator getIterator() {
return new ComposeIterator(menuItems.iterator());
}
@Override
public void print() {
System.out.println("****This is CakeHouseMenu****");
};
}
3、DinerMenu
代码语言:javascript复制public class DinerMenu extends MenuComponent {
private final static int Max_Items = 5;
private int numberOfItems = 0;
private MenuComponent[] menuItems;
public DinerMenu() {
menuItems = new MenuComponent[Max_Items];
addItem("vegetable Blt", "bacon&lettuce&tomato&cabbage", true, 3.58f);
addItem("Blt", "bacon&lettuce&tomato", false, 3.00f);
addItem("bean soup", "bean&potato salad", true, 3.28f);
addItem("hotdog", "onions&cheese&bread", false, 3.05f);
addSubMenu(new SubMenu());
}
private void addItem(String name, String description, boolean vegetable, float price) {
MenuItem menuItem = new MenuItem(name, description, vegetable, price);
if (numberOfItems >= Max_Items) {
System.err.println("sorry,menu is full!can not add another item");
} else {
menuItems[numberOfItems] = menuItem;
numberOfItems ;
}
}
private void addSubMenu(MenuComponent mMenuComponent) {
if (numberOfItems >= Max_Items) {
System.err.println("sorry,menu is full!can not add another item");
} else {
menuItems[numberOfItems] = mMenuComponent;
numberOfItems ;
}
}
}
4、SubMenu
代码语言:javascript复制public class SubMenu extends MenuComponent {
private ArrayList<MenuComponent> menuItems;
public SubMenu() {
menuItems = new ArrayList<MenuComponent>();
addItem("Apple Cookie", "Apple&candy&Cookie", true, 1.99f);
addItem("Banana Cookie", "Banana&candy&Cookie", false, 1.59f);
addItem("Orange Cookie", "Orange&Cookie", true, 1.29f);
}
private void addItem(String name, String description, boolean vegetable,float price) {
MenuItem menuItem = new MenuItem(name, description, vegetable, price);
menuItems.add(menuItem);
}
public Iterator getIterator() {
return new ComposeIterator(menuItems.iterator());
}
@Override
public void print() {
System.out.println("****This is SubMenu****");
};
}
5、MenuItem
代码语言:javascript复制public class MenuItem extends MenuComponent{
private String name,description;
private boolean vegetable;
private float price;
public MenuItem(String name,String description,boolean vegetable,float price){
this.name=name;
this.description=description;
this.vegetable=vegetable;
this.price=price;
}
@Override
public String getName()
{
return name;
}
@Override
public String getDescription()
{
return description;
}
@Override
public float getPrice()
{
return price;
}
@Override
public boolean isVegetable()
{
return vegetable;
}
@Override
public void print() {
System.out.println(getName() "***" getPrice() "***" getDescription());
}
}