组合模式

组合模式:允许你将对象组合成树形结构来表现“整体/部分”层次结构。组合能让客户以一致的方式处理个别对象以及对象组合。当你有数个对象的集合,他们彼此有“”整体/部分”的关系,并且你想要一致的方式对待这些对象时,就可以使用这种模式。常见的树形结构,父节点和叶子节点都可能有名称,描述等字段,但他们的实现方法却不一样。

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();	
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值