- 浏览: 198545 次
- 性别:
- 来自: 南京
文章分类
最新评论
-
yixinhu:
你好 能加下你的QQ么 我怎么打出来的exe点击都没反应啊.. ...
java 生成exe 文件 -
chenxiang105:
如果不需要flash 只需要做图片的翻页效果 这个是否也合适类 ...
jQuery插件page peel AD实现动态卷页、翻页或卷角flash广告效果 -
tuoxie007:
幸苦,好文章!
jetspeed门户项目组介绍 -
bobo:
需要考虑不同浏览器的兼容性
通过网页访问本地资源程序 -
tag13346:
说得这么玄,看下
时空趋势理论 --- 超越时空的均线技术(转载 )
允许你将对象组合成树形结构来表现“整体/部分”层次结构。组合能让客户以一致的方式处理个别对象以及对象组合。
创建一个树形结构,在同一个结构中处理嵌套菜单和菜单项组。通过将菜单和项放在相同的结构中,我们创建了一个“整体/部分”层次结构,即使由 菜单 和 菜单 项 组成 的 对象 树 。但是 可以 将 他视为 一个 整体,像是 一个 丰富的大菜单 。
1.我们应该努力让一个类只分配一个责任。
2.组合模式提供了一种结构,可同时包容个别对象和组合对象。
3。组合模式允许客户对个别对象和组合对象一视同仁。
4.组合结构内的任意对象称为组件,组件可以是组合,也可以是叶子节点。
5.在实现组合模式时,有许多设计上的 折中。你要根据需要平衡透明性和安全性。
package pattern; import java.util.ArrayList; import java.util.Iterator; import java.util.Stack; public class MenuTestDrive2 { public static void main(String[] args) { MenuComponent pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU","Breakfast"); 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,and sausage", true, 2.99)); pancakeHouseMenu.add(new MenuItem("BlueBerry Pancakes ", "Pancakes made with fresh blueberries", true, 3.49)); pancakeHouseMenu.add(new MenuItem("Waffles", "Waffles with your choice of blueberries or strawberries", true, 3.59)); MenuComponent dinerMenu = new Menu("DINER MENU","Lunch"); MenuComponent cafeMenu = new Menu("CAFE MENU","dINNER"); MenuComponent dessertMenu = new Menu("DESSERT MENU","Dessert of course!"); MenuComponent allMenus=new Menu("ALL MENUS","ALL menus combined"); allMenus.add(pancakeHouseMenu); allMenus.add(dinerMenu); allMenus.add(cafeMenu); cafeMenu.add(new MenuItem("Vegegie Burger and Air Fries", "Veggie burger on a whole wheat bun,lettuce,tomato,and fries", true, 3.99)); dinerMenu.add(new MenuItem("Soup of the day", "A cup of the soup of the day,with a side salad", true, 2.99)); dinerMenu.add(new MenuItem("Burrito", "A large burrito , with whole pinto beans,salsa,guacamole", true, 2.99)); dinerMenu.add(new MenuItem("Hotdog", "A hot dog, with saurkraut,relish,onions,topped with cheese", true, 2.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 ice cream",true,1.59)); Waiter waitress = new Waiter(allMenus); waitress.printMenu(); } } 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(){} public Iterator createIterator(){ throw new UnsupportedOperationException(); } } 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 boolean isVegetarian() { return vegetarian; } public double getPrice() { return price; } public void print(){ System.out.print(" "+getName()); if(this.isVegetarian()){ System.out.println("(v)"); } System.out.println(","+getPrice()); System.out.println(" ---"+getDescription()); } public Iterator createIterator(){ return new NullIterator(); } } 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 =(Iterator)menuComponents.iterator(); while(iterator.hasNext()){ MenuComponent menuComponent=(MenuComponent)iterator.next(); menuComponent.print(); } } public Iterator createIterator(){ return new CompositeIterator(menuComponents.iterator()); } } class CompositeIterator implements Iterator{ Stack stack=new Stack(); public CompositeIterator(Iterator iterator){ stack.push(iterator); } public Object next(){ if(hasNext()){ Iterator iterator=(Iterator)stack.peek(); MenuComponent component=(MenuComponent)iterator.next(); if(component instanceof Menu){ stack.push(component.createIterator()); } return component; }else{ return null; } } public boolean hasNext(){ if(stack.empty()){ return false; }else{ Iterator iterator =(Iterator)stack.peek(); if(!iterator.hasNext()){ stack.pop(); return hasNext(); }else{ return true; } } } public void remove(){ throw new UnsupportedOperationException(); } } class NullIterator implements Iterator{ public Object next(){ return null; } public boolean hasNext(){ return false; } public void remove(){ throw new UnsupportedOperationException(); } } class Waiter { MenuComponent allMenus; public Waiter(MenuComponent allMenus){ this.allMenus=allMenus; } public void printMenu() { this.allMenus.print(); } public void printVegetarianMenu(){ Iterator iterator=allMenus.createIterator(); System.out.println("\nVEGETARIAN MENU\n---"); while(iterator.hasNext()){ MenuComponent menuComponent=(MenuComponent)iterator.next(); try{ if(menuComponent.isVegetarian()){ menuComponent.print(); } } catch(UnsupportedOperationException e){} } } }
发表评论
-
Iterator patten 读书笔记
2008-04-10 18:07 1033提供 一种方法 顺序访问 一个聚和 对象中的各个元素,而又不暴 ... -
模版模式 读书笔记
2008-04-10 18:04 1710在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模版 ... -
Facade patten 读书笔记
2008-04-10 18:03 959提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一 ... -
Adapter patten 读书笔记
2008-04-10 18:01 927将一个类的接口,转换成客户期待的另一个接口。适配器让原本接口不 ... -
command patten 读书笔记
2008-04-10 17:56 8731 。命令模式将发出请求的对象和执行请求的对象解耦。 2。在被 ... -
单例模式 读书笔记
2008-04-07 18:44 884确保一个类只有一个实 ... -
抽象工厂模式读书笔记
2008-04-07 18:39 949提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具 ... -
工厂方法 读书笔记
2008-04-03 14:27 1108工厂发法定义了一个创建对象的接口或者抽象类,但是有子类来决定要 ... -
装饰者模式 读书笔记
2008-04-03 14:25 2106package pattern; public clas ... -
观察者模式(读书笔记)
2008-04-01 18:33 1078观察者模式定义了对象之间的一对多依赖,这样一来当一个对象改变状 ... -
Head First Design Patterns 读后感3-策略模式
2008-04-01 18:32 1305策略模式-定义了算法族,分别封装起来,让他们之间可以相互替换, ... -
Head First Design Patterns 读后感2 设计原则
2008-03-31 13:47 11351。软件开发中唯一一个不变的是 变化。大千世界,千奇百怪,行行 ... -
Head First Design Patterns 读后感1
2008-03-31 13:46 1387如何让大脑把记得更深刻 1。慢点,你理解的越多,记得月少 2。 ... -
proxy state 读书笔记
2008-03-28 18:35 1668定义:为另一个对象提供一个替身或者战位符以范围这个对象。 要点 ... -
阅读不懂,图书之过——《大话设计模式》创作历程 (转载)
2008-03-28 11:41 1091写这本书的确是个意外,因为在之前,我和所有的做技术的朋友一样, ... -
state patten 读书笔记
2008-03-25 14:53 1183允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的 ...
相关推荐
移动应用 UI 设计, 读书笔记, 记录摘取书籍中重点信息,有助于页面设计,原型设计等
设计模式1,包含了设计模式的基本介绍,以及状态机模式的读书笔记
USB读书笔记 USB读书笔记 USB读书笔记 USB读书笔记
JAVA模式笔记,JAVA模式的读书笔记,JAVA模式的读书笔记。
HeadFirst设计模式 读书 笔记
《嵌入式Linux C语言应用程序设计》读书笔记《嵌入式Linux C语言应用程序设计》读书笔记《嵌入式Linux C语言应用程序设计》读书笔记《嵌入式Linux C语言应用程序设计》读书笔记《嵌入式Linux C语言应用程序设计》...
计算机技术的读书笔记10篇计算机技术的读书笔记10篇计算机技术的读书笔记10篇计算机技术的读书笔记10篇计算机技术的读书笔记10篇
毕业论文读书笔记格式,教会你怎么写毕业论文读书笔记格式
学生读书笔记共享-学生读书笔记共享系统-学生读书笔记共享系统源码-学生读书笔记共享管理系统-学生读书笔记共享管理系统java代码-学生读书笔记共享系统设计与实现-基于springboot的学生读书笔记共享系统-基于Web的...
读书笔记读书笔记读书笔记
学生读书笔记共享-学生读书笔记共享系统-学生读书笔记共享系统源码-学生读书笔记共享管理系统-学生读书笔记共享管理系统java代码-学生读书笔记共享系统设计与实现-基于springboot的学生读书笔记共享系统-基于Web的...
深入浅出MFC读书笔记3 深入浅出MFC 读书笔记
深入浅出MySQL-读书笔记byCZF深入浅出MySQL-读书笔记byCZF深入浅出MySQL-读书笔记byCZF
地理信息系统概论_读书笔记_黄杏元_地理信息系统概论_读书笔记_黄杏元_地理信息系统概论_读书笔记_黄杏元_地理信息系统概论_读书笔记_黄杏元_地理信息系统概论_读书笔记_黄杏元_地理信息系统概论_读书笔记_黄杏元_
r和统计学读书笔记 r和统计学读书笔记 r和统计学读书笔记 r和统计学读书笔记 r和统计学读书笔记
计算机书籍读书笔记参考.docx