当前位置: 首页 > news >正文

《HeadFirst设计模式(第二版)》第九章代码——组合模式

上一章链接:

《HeadFirst设计模式(第二版)》第九章代码——迭代器模式_轩下小酌的博客-CSDN博客

        前面说到,当一个菜单里面出现了子菜单的时候,前面的迭代器模式得换成组合模式。

组合模式:

        允许将对象组合成树形结构来表现部分-整体层次结构。组合让用户可以统一处理个别对象和对象组合。

代码文件结构:

 

MenuComponent
package Chapter9_CompositePattern;/*** @Author 竹心* @Date 2023/8/18**/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();}
}
MenuItem
package Chapter9_CompositePattern;/*** @Author 竹心* @Date 2023/8/18**/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());}
}
Menu
package Chapter9_CompositePattern;import java.util.ArrayList;
import java.util.Iterator;/*** @Author 竹心* @Date 2023/8/18**/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 (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<MenuComponent> iterator = menuComponents.iterator();while (iterator.hasNext()) {MenuComponent menuComponent =(MenuComponent)iterator.next();menuComponent.print();}}
}
Waitress 
package Chapter9_CompositePattern;/*** @Author 竹心* @Date 2023/8/18**/public class Waitress {MenuComponent allMenus;//菜单根节点public Waitress(MenuComponent allMenus) {this.allMenus = allMenus;}public void printMenu() {allMenus.print();}
}
MenuTestDrive
package Chapter9_CompositePattern;/*** @Author 竹心* @Date 2023/8/18**/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));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));cafeMenu.add(coffeeMenu);coffeeMenu.add(new MenuItem("Coffee Cake","Crumbly cake topped with cinnamon and walnuts",true,1.59));Waitress waitress = new Waitress(allMenus);//这示例代码怎么这么多。。。waitress.printMenu();}
}

http://www.lryc.cn/news/137343.html

相关文章:

  • iOS17 widget Content margin
  • 计网第四章(网络层)(一)
  • 【前端】vue3 接入antdv表单校验
  • CY3-COOH在蛋白质定位的特点1251915-29-3星戈瑞
  • 数据采集:selenium 获取某网站CDN 商家排名信息
  • 5.从头跑一个pipeline
  • leetcode原题: 堆箱子(动态规划实现)
  • Java中数组和集合的对比,以及什么情况下使用数组更合适,什么情况下使用集合更合适。集合的基本介绍和集合体系图。
  • STM32之17.PWM脉冲宽度调制
  • VS2015打开Qt的pro项目文件 报错
  • 骨传导耳机会头疼吗?骨传导耳机会对身体不好吗
  • 【面试题系列】(一)
  • vscode C++17便捷配置教程(懒人版)
  • 动态数组实现链地址法哈希表
  • Eclipse(STS):pom.xml 报错:Multiple markers at this line
  • CSerialPort教程4.3.x (3) - CSerialPort在MFC中的使用
  • 2022版 的IDEA创建一个maven项目(超详细)
  • lvs实现DR模型搭建
  • 设计模式之迭代器模式(Iterator)的C++实现
  • 【0基础入门Python Web笔记】二、python 之逻辑运算和制流程语句
  • 容器——Docker
  • SQL注入之宽字节注入
  • MyBatis动态sql
  • L1-032 Left-pad 测试点全过
  • ssm+Vue.js在线购物系统源码和论文
  • 港联证券|指数或进入磨底阶段 短期关注环保、煤炭等板块
  • pytorch 实现VGG
  • 科技项目验收检测报告获取有哪些注意事项,作用都有哪些?
  • OceanBase:谁动了我得参数?
  • Python快速入门体验