设计模式—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();


	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值