组合模式(Composite Pattern)(二):HeadFirst中使用组合设计菜单

一、问题描述


使用组合模式为餐厅设计菜单,使服务员Waitress可以很方便地使用菜单。



二、类图




三、代码实现



1.(Component角色)MenuComponent

public abstract class MenuComponent
{

	//对一些方法进行了默认实现
	public void add(MenuComponent menuComponent)
	{
		throw new UnsupportedOperationException();
	}

	public void remove(MenuComponent menuComponent)
	{
		throw new UnsupportedOperationException();
	}

	public MenuComponent getChild(int i)
	{
		throw new UnsupportedOperationException();
	}

	public String getName()
	{
		throw new UnsupportedOperationException();
	}

	public String getDescription()
	{
		throw new UnsupportedOperationException();
	}

	public double getPrice()
	{
		throw new UnsupportedOperationException();
	}

	public boolean isVegetarian()
	{
		throw new UnsupportedOperationException();
	}

	public void print()
	{
		throw new UnsupportedOperationException();
	}
}

2.(Leaf角色)MenuItem

public class MenuItem extends MenuComponent
{
	String name;
	String description;
	boolean vegetarian;
	double price;

	public MenuItem(String name, String description, 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.print("  " + getName());
		if (isVegetarian())//素食
		{
			System.out.print("(v)");
		}
		System.out.println(", " + getPrice());
		System.out.println("     -- " + getDescription());
	}
}

3.(Composite角色)Menu

public class Menu extends MenuComponent
{
	ArrayList<MenuComponent> menuComponents = new ArrayList<MenuComponent>();
	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  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());
		System.out.println("---------------------");

		Iterator iterator = menuComponents.iterator();
		while (iterator.hasNext())
		{
			MenuComponent menuComponent = (MenuComponent) iterator.next();
			menuComponent.print();
		}
	}
}

4.(Client角色)Waitress

public class Waitress
{
	MenuComponent allMenus;

	public Waitress(MenuComponent allMenus)
	{
		this.allMenus = allMenus;
	}

	public void printMenu()
	{
		allMenus.print();
	}
}

5.测试略



转载请注明出处:http://blog.csdn.net/jialinqiang/article/details/8947935

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值