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

哈尔滨建设局网站建设装饰网站

哈尔滨建设局网站,建设装饰网站,吉安好的网站建设公司,手把手教你网上开店上一章链接: 《HeadFirst设计模式(第二版)》第九章代码——迭代器模式_轩下小酌的博客-CSDN博客 前面说到,当一个菜单里面出现了子菜单的时候,前面的迭代器模式得换成组合模式。 组合模式: 允许将对象组合成树形结构来表现部分-整…

上一章链接:

《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.yayakq.cn/news/646516/

相关文章:

  • 360免费自助建站wordpress非凡主题里导航下拉菜单
  • 12380网站建设情况说明各类网站推广
  • 个人网站鉴赏网站建设要什么
  • 银行网站建设方案视频网站建设目的和意义
  • 网站负责人拍照小学生摘抄新闻
  • 网站被降权表现在线教育平台搭建
  • seo资料站wordpress悬浮小工具的插件
  • 广东网站制作公司酒店手机网站模板
  • 楚雄企业网站建设公司直播营销
  • 网页网站公司如何做备份2018年网站开发技术
  • 加盟做网站公司名称起名大全
  • 西宁网站建设有限公司山东网站建设设计
  • 杭州集团网站建设方案百度关键词搜索次数
  • 网站分析与优化的文章餐饮网站建设
  • 涡阳网站建设哪家好查询工具类网站制作
  • 织梦网站怎么做301跳转广州网站建设网页设计
  • 南充市住房和城乡建设厅官方网站网站建设 百度云盘
  • 商城网站的psd模板免费下载好网站制作
  • 大型营销型网站制作网站服务器租用多少钱
  • app哪个网站开发好好用的wordpress模板
  • 哈尔滨网站制作工程用什么工具建设网站
  • 余杭区网站建设微网站开发系统
  • 网站关键词排名优化工具四川新站优化
  • ps网站设计概述炉石卡牌制作网页
  • 深圳网站建设定制平台wordpress portfolio
  • 网站制作ppt模板wordpress 表格 链接地址
  • 更合网站开发门户网站建设如何入账
  • 电商网站开发教材营销型网站名词解释
  • 编写网站方案设计书表格什么建设网站
  • 中企动力近期做的网站广告公司广告设计