组合模式:
package com.hycz.design.pattern.Composite;
/**
* Created with IntelliJ IDEA.
* User: shangke
* Date: 6/17/13
* Time: 4:38 PM
* To change this template use File | Settings | File Templates.
*/
public class MenuComponent {
public void add(MenuComponent menuComponent) {}
public void remove(MenuComponent menuComponent){}
public MenuComponent getChild(int i){
return null;
}
public String getName(){
return "";
}
public String getDescription(){
return "";
}
public double getPrice(){
return 0;
}
public boolean isVegetarian(){
return false;
}
public void print(){
}
}
package com.hycz.design.pattern.Composite;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: shangke
* Date: 6/17/13
* Time: 4:44 PM
* To change this template use File | Settings | File Templates.
*/
public class Menu extends MenuComponent {
private List<MenuComponent> menuComponents = new ArrayList<MenuComponent>();
private String name;
private String description;
public Menu(String name, String description) {
this.name = name;
this.description = description;
}
@Override
public void add(MenuComponent menuComponent) {
menuComponents.add(menuComponent); //To change body of overridden methods use File | Settings | File Templates.
}
@Override
public void remove(MenuComponent menuComponent) {
menuComponents.remove(menuComponent); //To change body of overridden methods use File | Settings | File Templates.
}
@Override
public MenuComponent getChild(int i) {
return menuComponents.get(i); //To change body of overridden methods use File | Settings | File Templates.
}
@Override
public String getName() {
return name; //To change body of overridden methods use File | Settings | File Templates.
}
@Override
public String getDescription() {
return description; //To change body of overridden methods use File | Settings | File Templates.
}
@Override
public void print() {
System.out.println(getName());
System.out.println(", " + getDescription());
System.out.println("--------------------");
Iterator<MenuComponent> iterator = menuComponents.iterator();
while(iterator.hasNext()){
MenuComponent menuComponent = iterator.next();
menuComponent.print();
}
}
}
package com.hycz.design.pattern.Composite;
/**
* Created with IntelliJ IDEA.
* User: shangke
* Date: 6/17/13
* Time: 4:45 PM
* To change this template use File | Settings | File Templates.
*/
public class MenuItem extends MenuComponent {
private String name;
private String description;
private double price;
private boolean isVegetarain;
public MenuItem(String name, String description, double price, boolean vegetarain) {
this.name = name;
this.description = description;
this.price = price;
isVegetarain = vegetarain;
}
@Override
public String getName() {
return name; //To change body of overridden methods use File | Settings | File Templates.
}
@Override
public String getDescription() {
return description; //To change body of overridden methods use File | Settings | File Templates.
}
@Override
public double getPrice() {
return price; //To change body of overridden methods use File | Settings | File Templates.
}
@Override
public boolean isVegetarian() {
return isVegetarain; //To change body of overridden methods use File | Settings | File Templates.
}
@Override
public void print() {
System.out.print(" " + getName());
if (isVegetarian()) System.out.printf("(v)");
System.out.println("price = " + getPrice());
System.out.println("description = " + getDescription());
}
}
package com.hycz.design.pattern.Composite;
/**
* Created with IntelliJ IDEA.
* User: shangke
* Date: 6/17/13
* Time: 4:58 PM
* To change this template use File | Settings | File Templates.
*/
public class Waitress {
MenuComponent allMenus;
public Waitress(MenuComponent allMenus) {
this.allMenus = allMenus;
}
public void printMenu(){
allMenus.print();
}
}
package com.hycz.design.pattern.Composite;
/**
* Created with IntelliJ IDEA.
* User: shangke
* Date: 6/17/13
* Time: 4:45 PM
* To change this template use File | Settings | File Templates.
*/
public class CompositeMain {
public static void main(String[] args) {
MenuComponent pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU","Breakfast");
MenuComponent dinnerMenu = new Menu("DINNER 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 combeind");
allMenus.add(pancakeHouseMenu);
allMenus.add(dinnerMenu);
allMenus.add(cafeMenu);
//加入菜单项
dinnerMenu.add(new MenuItem("Pasta","Pasta xxxxx",3.98,true));
dinnerMenu.add(dessertMenu);
dessertMenu.add(new MenuItem("Apple pie","Apple pie xxxxxxx",0.98,false));
Waitress waitress = new Waitress(allMenus);
waitress.printMenu();
}
}