`
pan_java
  • 浏览: 287925 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

hand first 设计模式 -组合模式-1

阅读更多
组合模式:允许你将对象组成树形结构来表现"整体/部份"的层次结构.组合能让客户以一致的方式处理个别对象和对象组合.

下面程序的目的是打印所有菜单和子菜单的信息.

菜单和子菜单都继承自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();
	}
}
分享到:
评论

相关推荐

    php设计模式-策略模式-例题学习

    在IT行业中,设计模式是软件开发中的重要概念,它们代表了在特定场景下解决常见问题的最佳实践。策略模式是设计模式的一种,它允许我们在运行时动态地改变对象的行为。在这个"php设计模式-策略模式-例题学习"的...

    tv-w07-my-firsthand-experience-with-ransomware_copy1.zip

    标题“tv-w07-my-firsthand-experience-with-ransomware_copy1.zip”暗示了这是一个关于个人经历与勒索软件(Ransomware)遭遇的故事。勒索软件是一种恶意软件,它会加密用户的重要文件,然后要求支付赎金以解密这些...

    my-firsthand-experience-with-ransomware_copy1.pdf

    描述:“my-firsthand-experience-with-ransomware_copy1.pdf”文档是由Capital One公司的网络安全部门经理Birat Niraula所作的一次演讲,他详细介绍了自己与勒索软件的亲身经历,以及勒索软件的攻击途径、近期的...

    HAND技术顾问培训-SQL练习题_v1.0及答案

    例如,`SELECT column1, column2 FROM table WHERE condition` 是最基础的查询模式,用于从特定表中选取满足条件的数据。 2. 数据检索:在实践中,你需要学会如何从一个或多个表中检索数据。这涉及到对JOIN操作的...

    GM-HandTool-2019-05_handtool_

    "GM-HandTool-2019-05"便是这样一款专为MapleStory(枫之谷)游戏设计的实用工具集,它旨在帮助玩家和开发者更好地管理和修改游戏文件,从而提升游戏体验。 MapleStory,这款由Nexon开发并发行的2D横向卷轴式网络...

    UL 60745-1-2022-06-11 Hand-Held Motor-Operated Electric Tools

    UL 60745-1-2022-06-11 Hand-Held Motor-Operated Electric Tools

    Auto Hand-3.2 - VR Physics Interaction

    Auto Hand-3.2 - VR Physics Interaction 测试可用

    短学期课程设计-数据库原理_Second-hand-book-trading-system.zip

    短学期课程设计-数据库原理_Second-hand-book-trading-system

    信息安全_数据安全_tv-w07-my-firsthand-experience-w.pdf

    本文主要聚焦于一个具体的安全威胁——勒索软件(Ransomware),并通过名为“TV-W07-my-firsthand-experience-w”的PDF文档,分享了Birat Niraula先生在Capital One任职期间对这一威胁的亲身经历和应对策略。...

    Hand-Drawn-Shader-Pack-V1.2.zip

    "Hand-Drawn-Shader-Pack-V1.2.zip"正是一个专为Unity3D设计的手绘风格化Shader资源包,旨在帮助开发者轻松地将游戏或应用呈现出独特的手绘艺术风格。 手绘风格化Shader是一种模拟传统绘画技巧的技术,它可以使3D...

    real-hand-held-20200511T154014Z-001.zip

    在本文中,我们将深入探讨一个名为“real-hand-held”的背景抠图模型,其相关权重文件存储于“real-hand-held-20200511T154014Z-001.zip”压缩包中。这个模型专门针对手持物体的情况,提供高质量的抠图效果。 首先...

    -matlab-hand-written-num-recognization-master.zip

    -matlab-hand-written-num-recognization-master

    jawide-hand-gesture-recognition-master.zip

    《基于jawide-hand-gesture-recognition的三种手势识别技术详解》 在当今信息化社会,人工智能(AI)已经深入到我们生活的各个角落,其中,手势识别技术作为一种非接触式的交互方式,逐渐受到广泛关注。本篇文章将...

    AS-23-Pinto-Hand-Me-Your-Secret-MCU.pdf

    AS-23-Pinto-Hand-Me-Your-Secret-MCU

    imclab-Hand-Geometry-Recognition-System-Matlab-Code

    【标题】"imclab-Hand-...综上所述,"imclab-Hand-Geometry-Recognition-System-Matlab-Code"项目涵盖了图像处理、模式识别、机器学习等多个领域的知识点,对于想深入了解这些领域的开发者来说,这是一个宝贵的资源。

    keras-yolo3-recognize.rar

    hand-keras-yolo3-recognize 模型训练参考:https://gitee.com/cungudafa/keras-yolo3 yolo3识别这里参考于:https://github.com/AaronJny/tf2-keras-yolo3

    基于LeftHand的IP-SAN解决方案

    基于LeftHand的IP-SAN(Internet Protocol Storage Area Network)方案就是针对这样的需求而设计的。IP-SAN是一种利用IP网络来实现存储区域网络的技术,它允许企业通过标准网络基础设施进行数据存储和管理。 第二章...

    Building State Machine Workflows with WF Hand-on Lab - Source code

    This is a step by step hand-on lab for WF from Microsoft Switzerland. In this Hands-On-Lab you will create a State Machine Workflow and integrate it in an existing ASP.NET Web Application.

    Hamilton-Hand-Drawn-Uppercase-Font_hamilton_exerciseihy_presenta

    标题 "Hamilton-Hand-Drawn-Uppercase-Font_hamilton_exerciseihy_presenta" 暗示我们正在处理一个与字体设计相关的资源,特别是以“Hamilton”为名的手绘大写字体。这个标题可能代表了一种设计风格,其中“Hand-...

Global site tag (gtag.js) - Google Analytics