《Head First 设计模式》学习笔记——迭代模式 + 组合模式
这样简化了聚合的接口和实现。也让责任各得其所。
换句话说,在大多数情况下。我们能够忽略对象组合和个别对象之间的区别。
//定义迭代器接口
public interface Iterator {
boolean hasNext();
Object next();
} // implements 实现详细接口
public class DinerMenuIterator implements Iterator {
MenuItem[] items;
int position = 0; public DinerMenuIterator(MenuItem[] items) {
this.items = items;
} public Object next() {
MenuItem menuItem = items[position];
position = position + 1;
return menuItem;
} public boolean hasNext() {
if (position >= items.length || items[position] == null) {
return false;
} else {
return true;
}
}
} public class DinerMenu implements Menu {
static final int MAX_ITEMS = 6;
int numberOfItems = 0;
MenuItem[] menuItems; public DinerMenu() {
menuItems = new MenuItem[MAX_ITEMS]; addItem("Vegetarian BLT",
"(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99);
addItem("BLT",
"Bacon with lettuce & tomato on whole wheat", false, 2.99);
addItem("Soup of the day",
"Soup of the day, with a side of potato salad", false, 3.29);
addItem("Hotdog",
"A hot dog, with saurkraut, relish, onions, topped with cheese",
false, 3.05);
addItem("Steamed Veggies and Brown Rice",
"Steamed vegetables over brown rice", true, 3.99);
addItem("Pasta",
"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
true, 3.89);
} public void addItem(String name, String description,
boolean vegetarian, double price)
{
MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
if (numberOfItems >= MAX_ITEMS) {
System.err.println("Sorry, menu is full! Can't add item to menu");
} else {
menuItems[numberOfItems] = menuItem;
numberOfItems = numberOfItems + 1;
}
} //返回迭代器接口
public Iterator createIterator() {
return new DinerMenuIterator(menuItems);
} // other menu methods here
} package net.dp.iterator.dinermerger; public class Waitress {
PancakeHouseMenu pancakeHouseMenu;
DinerMenu dinerMenu; //在构造器中,女招待照应两个菜单
public Waitress(PancakeHouseMenu pancakeHouseMenu, DinerMenu dinerMenu) {
this.pancakeHouseMenu = pancakeHouseMenu;
this.dinerMenu = dinerMenu;
} public void printMenu() {
//为每个菜单各自创建一个迭代器
Iterator pancakeIterator = pancakeHouseMenu.createIterator();
Iterator dinerIterator = dinerMenu.createIterator(); System.out.println("MENU\n----\nBREAKFAST");
//对每个迭代器调用重载printMenu(),将迭代器传入
printMenu(pancakeIterator);
System.out.println("\nLUNCH");
printMenu(dinerIterator);
} private void printMenu(Iterator iterator) {
while (iterator.hasNext()) {
MenuItem menuItem = (MenuItem)iterator.next();
System.out.print(menuItem.getName() + ", ");
System.out.print(menuItem.getPrice() + " -- ");
System.out.println(menuItem.getDescription());
}
} // other methods here
}
//MenuComponent对每一个方法都提供了默认的实现
public abstract class MenuComponent { public void add(MenuComponent 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 abstract Iterator<MenuComponent> createIterator(); public void print() {
throw new UnsupportedOperationException();
}
} //首先扩展MenuComponent接口,实现菜单项
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 Iterator<MenuComponent> createIterator() {
return new NullIterator();
} //对于菜单项来说,此方法会打印出完整的菜单项条目
public void print() {
System.out.print(" " + getName());
if (isVegetarian()) {
System.out.print("(v)");
}
System.out.println(", " + getPrice());
System.out.println(" -- " + getDescription());
}
// vv MenuItemCompositeV2Main
} //实现组合菜单
public class Menu extends MenuComponent {
//菜单能够有随意数目的孩子,这些孩子都必须属于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 Iterator<MenuComponent> createIterator() {
return new CompositeIterator(menuComponents.iterator());
} 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 =
iterator.next();
menuComponent.print();
}
}
} public class Waitress {
MenuComponent allMenus; //仅仅须要将最顶层菜单交给招待即可
public Waitress(MenuComponent allMenus) {
this.allMenus = allMenus;
} public void printMenu() {
allMenus.print();
} public void printVegetarianMenu() {
Iterator<MenuComponent> iterator = allMenus.createIterator(); System.out.println("\nVEGETARIAN MENU\n----");
while (iterator.hasNext()) {
MenuComponent menuComponent = iterator.next();
try {
//我们调用全部的menuComponent的isVegetarian方法。可是Menu会抛出一个异常。由于他们不支持这个操作
if (menuComponent.isVegetarian()) {
menuComponent.print();
}
} catch (UnsupportedOperationException e) {
//假设菜单组件不支持这个操作,我们就对这个异常置之不理。
}
}
}
} package net.dp.composite.menuiterator; 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 allMenus = new Menu("ALL MENUS", "All menus combined"); //使用组合的add方法,将每一个菜单增加到顶层菜单中
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)); //增加很多其它的菜单项 //一旦将整个財大层次构造万别。将它交给女招待
Waitress waitress = new Waitress(allMenus); waitress.printVegetarianMenu(); }
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
《Head First 设计模式》学习笔记——迭代模式 + 组合模式的更多相关文章
- C#设计模式学习笔记:(9)组合模式
本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7743118.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲结构型设计模式的第四个模式--组 ...
- 【设计模式】学习笔记13:组合模式(Composite)
本文出自 http://blog.csdn.net/shuangde800 认识组合模式 上一篇中,我们可以用迭代器来实现遍历一个集合(数组,ArrayList, Vector, HashTabl ...
- Java设计模式学习笔记(四) 抽象工厂模式
前言 本篇是设计模式学习笔记的其中一篇文章,如对其他模式有兴趣,可从该地址查找设计模式学习笔记汇总地址 1. 抽象工厂模式概述 工厂方法模式通过引入工厂等级结构,解决了简单工厂模式中工厂类职责太重的问 ...
- Java设计模式学习笔记(二) 简单工厂模式
前言 本篇是设计模式学习笔记的其中一篇文章,如对其他模式有兴趣,可从该地址查找设计模式学习笔记汇总地址 正文开始... 1. 简介 简单工厂模式不属于GoF23中设计模式之一,但在软件开发中应用也较为 ...
- Java设计模式学习笔记(三) 工厂方法模式
前言 本篇是设计模式学习笔记的其中一篇文章,如对其他模式有兴趣,可从该地址查找设计模式学习笔记汇总地址 1. 简介 上一篇博客介绍了简单工厂模式,简单工厂模式存在一个很严重的问题: 就是当系统需要引入 ...
- C#设计模式学习笔记:(21)访问者模式
本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/8135083.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲行为型设计模式的第九个模式--访 ...
- C#设计模式学习笔记:(15)迭代器模式
本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7903617.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲行为型设计模式的第三个模式--迭 ...
- C#设计模式学习笔记:(7)桥接模式
本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7699301.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲结构型设计模式的第二个模式--桥 ...
- C#设计模式学习笔记:(23)解释器模式
本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/8242238.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲行为型设计模式的第十一个模式-- ...
- C#设计模式学习笔记:(19)策略模式
本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/8057654.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲行为型设计模式的第七个模式--策 ...
随机推荐
- Delphi的VMT的结构图,很清楚
Every Delphi class is defined internally by its vmt—its virtual-method table. The vmt contains a li ...
- 96 Stocks APIs: Bloomberg, NASDAQ and E*TRADE
Our API directory now includes 96 stocks APIs. The newest is the Eurex VALUES API. The most popula ...
- c语言实现动态指针数组Dynamic arrays
c语言实现动态数组.其它c的数据结构实现,hashTable參考点击打开链接 treeStruct參考点击打开链接 基本原理:事先准备好一个固定长度的数组. 假设长度不够的时候.realloc一块区域 ...
- openwrt 3g模块上网
硬件环境: 开发板为RT5053F 3G模块为中兴 MC2176 电信版 以下是操作步骤 加入VID .PID VID . PID 的获取方法是 将设备插入电脑在linux下执行 ...
- IP分类地址——a,b,c 类是如何划分的
今天IP网络使用32位地址,点分十进制格式,如172.16.0.0.地址格式:IP地址=网络地址+主机地址 或 IP地址=主机地址+子网地址+主机地址. IP地址类型 当互联网最初的设计,为了便于网络 ...
- IP Editor IP控件(对比一下封装IP控件)
HWND hIpEdit; void __fastcall TForm2::FormCreate(TObject *Sender) { hIpEdit = CreateWindow(WC_IPADDR ...
- That's life,多一些韧性,才有更多的任性(转)
如果是正确的选择,就不要遵守太多规则. 若有容纳之心,便丰富了自己,也闪了他人,平常心,平常事 阅读,是保持时尚最节约的方式,也是快乐的源泉.可人生难免失意,有了快乐的能力,还应有面对沮丧的心胸. 相 ...
- http调试工具Charles Proxy用法详解
Charles Proxy 通常称为Charles,Charles是目前最强大的http调试工具,在界面和功能上远强于Fiddler,同时是全平台支持,堪称圣杯级工具,不过在这里为您提供了Charle ...
- grep与正则表达式,grep、egrep和fgrep
grep用法详解:grep与正则表达式 首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串. ...
- hdu4496 D-City
D-City Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submis ...