設計模式—Composite代碼

public abstract class MenuComponent{
	public void add(MenuComponent menuComponent) {
		
	}
	public void remove(MenuComponent menuComponent) {
		
	}
	public void getComponent getChild(int i) {
		
	}
	
	public String getName() {
	
	}
	
	public String getDescription() {
		
	}	
	
	public double getPrice() {
		
	}
	
	public boolean isVegetarian() {
		
	}
}
public class MenuItem extends MenuComponent {
		String name;
		String description;
		boolean vegetarian;
		double price;
		
		public MenuItem(String name,String descrption,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.println("(v)");
			}
			System.out.println(" "+getPrice());
			System.out.println(" " + getDescription());
		}
}

public class Menu extends MenuComponent {
	ArrayList menuComponents = new ArrayList();
	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.print("\n" + getName());
		System.out.println(", " + getDescription());
		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 coffeeMenu = new Menu("COFFEE MENU", "Stuff for the afternoon coffee");
		MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined");
		allMenus.add(pancakeHouseMenu);
		allMenus.add(dinerMenu);
		allMenus.add(cafeMenu);
		pancakeHouseMenu.add(new MenuItem( "K&B's Pancake Breakfast","Pancakes with scrambled eggs, and toast", true, 2.99));
		dinerMenu.add(new MenuItem( "Vegetarian BLT",
		"(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99));
		dinerMenu.add(dessertMenu);
		dessertMenu.add(new MenuItem( "Apple Pie",
		"Apple pie with a flakey crust, topped with vanilla icecream", true, 1.59));
		
		Waitress waitress = new Waitress(allMenus);
		waitress.printMenu();


	}
	
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章