组合模式:允许你将对象组成树形结构来表现"整体/部份"的层次结构.组合能让客户以一致的方式处理个别对象和对象组合.
下面程序的目的是打印所有菜单和子菜单的信息.
菜单和子菜单都继承自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();
}
}
菜单
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());
System.out.println("---------------------");
Iterator iterator = menuComponents.iterator();
while (iterator.hasNext()) {
MenuComponent menuComponent =
(MenuComponent)iterator.next();
//如果是子菜单会利用递归的方式继续子菜单的订单信息.和子菜单全部菜单项的信息.如果是菜单项直接输出信息.关键就是菜单和菜单项作为同一类型的对象处理.给程序带来很大的简便性
menuComponent.print();
}
}
}
//菜单项
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());
}
}
//服务员
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 to go with your 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));
pancakeHouseMenu.add(new MenuItem(
"Regular Pancake Breakfast",
"Pancakes with fried eggs, sausage",
false,
2.99));
pancakeHouseMenu.add(new MenuItem(
"Blueberry Pancakes",
"Pancakes made with fresh blueberries, and blueberry syrup",
true,
3.49));
pancakeHouseMenu.add(new MenuItem(
"Waffles",
"Waffles, with your choice of blueberries or strawberries",
true,
3.59));
dinerMenu.add(new MenuItem(
"Vegetarian BLT",
"(Fakin') Bacon with lettuce & tomato on whole wheat",
true,
2.99));
dinerMenu.add(new MenuItem(
"BLT",
"Bacon with lettuce & tomato on whole wheat",
false,
2.99));
dinerMenu.add(new MenuItem(
"Soup of the day",
"A bowl of the soup of the day, with a side of potato salad",
false,
3.29));
dinerMenu.add(new MenuItem(
"Hotdog",
"A hot dog, with saurkraut, relish, onions, topped with cheese",
false,
3.05));
dinerMenu.add(new MenuItem(
"Steamed Veggies and Brown Rice",
"Steamed vegetables over brown rice",
true,
3.99));
dinerMenu.add(new MenuItem(
"Pasta",
"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
true,
3.89));
dinerMenu.add(dessertMenu);
dessertMenu.add(new MenuItem(
"Apple Pie",
"Apple pie with a flakey crust, topped with vanilla icecream",
true,
1.59));
dessertMenu.add(new MenuItem(
"Cheesecake",
"Creamy New York cheesecake, with a chocolate graham crust",
true,
1.99));
dessertMenu.add(new MenuItem(
"Sorbet",
"A scoop of raspberry and a scoop of lime",
true,
1.89));
cafeMenu.add(new MenuItem(
"Veggie Burger and Air Fries",
"Veggie burger on a whole wheat bun, lettuce, tomato, and fries",
true,
3.99));
cafeMenu.add(new MenuItem(
"Soup of the day",
"A cup of the soup of the day, with a side salad",
false,
3.69));
cafeMenu.add(new MenuItem(
"Burrito",
"A large burrito, with whole pinto beans, salsa, guacamole",
true,
4.29));
cafeMenu.add(coffeeMenu);
coffeeMenu.add(new MenuItem(
"Coffee Cake",
"Crumbly cake topped with cinnamon and walnuts",
true,
1.59));
coffeeMenu.add(new MenuItem(
"Bagel",
"Flavors include sesame, poppyseed, cinnamon raisin, pumpkin",
false,
0.69));
coffeeMenu.add(new MenuItem(
"Biscotti",
"Three almond or hazelnut biscotti cookies",
true,
0.89));
Waitress waitress = new Waitress(allMenus);
waitress.printMenu();
}
}
分享到:
- 2009-07-03 11:44
- 浏览 966
- 评论(0)
- 论坛回复 / 浏览 (0 / 1875)
- 查看更多
相关推荐
**Hand(手)** - 鼠标指针的样式之一,当用户将鼠标悬停在链接上时显示。 - 可以通过`cursor: pointer`设置。 **Image(图像)** - 图片或照片,可以作为背景或嵌入内容。 - 通过`<img>`标签或`background-image`...
在CSS中用于表示颜色或图像的透明程度,尤其是在处理颜色的RGBA模式时。 - **Anchor**: 锚点。HTML中的`<a>`标签即代表此单词的缩写,用于创建超链接。 - **Arrow**: 箭头。在CSS中,可以通过`::before`或`::after`...
10. pattern ['pætən] - 模式:可以指设计、行为习惯或一种可预见的结构,如:“他的学习模式非常有效。” 11. guesser ['gesə] - 猜测者:指经常进行猜测或尝试预测结果的人。 12. clue [klu:] - 线索:在解决...
ASCII是一种基于拉丁字母的一套电脑编码系统,最初被设计为7位的编码并被国际标准化组织(ISO)指定为国际标准ISO 646。在SIEMENS CNC系统中,ASCII用于数据的传输与处理,确保了信息在不同系统间的兼容性。 #### ...
void free(void *firstbyte):如果给定一个由先前的 malloc 返回的指针,那么该函数会将分配的空间归还给进程的“空闲空间”。 malloc_init 将是初始化内存分配程序的函数。它要完成以下三件事:将分配程序...
1. **FIFO (First In First Out)**:FIFO是一种先进先出的数据缓冲区,用于临时存储数据,等待传输。在USB通信中,FIFO用于暂存从主机发送过来的命令或要回传的数据。 2. **Control Read**:控制读取是控制传输的一...
void free(void *firstbyte):如果给定一个由先前的 malloc 返回的指针,那么该函数会将分配的空间归还给进程的“空闲空间”。 malloc_init 将是初始化内存分配程序的函数。它要完成以下三件事:将分配程序标识为...
// 打开模式 if (dlg.DoModal() == IDOK) { // 文件路径 CString filePath = dlg.GetPathName(); // 进一步处理文件路径 } } ``` #### 九、窗口居中 要使窗口居中显示,可以在窗口初始化时计算屏幕中心位置...
C#编程经验技巧宝典源代码,目录如下: 第1章 开发环境 1 <br>1.1 Visual Studio开发环境安装与配置 2 <br>0001 安装Visual Studio 2005开发环境须知 2 <br>0002 配置合适的Visual Studio 2005...