设计模式C#实现(四)——迭代器模式
迭代器模式:提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。
UML类图:

煎饼屋和餐厅合并了!但是有个小问题,虽然两家都同意实现相同的菜单项MenuItem,但是煎饼屋想使用ArrayList储存菜单项,而餐厅则使用数组,为了使女招待能同时访问两家的菜单,我们需要为菜单提供一个统一的访问接口。
先来看菜单项MenuItem,两家店的实现相同
class MenuItem
{
string name;//名称
string description;//描述
bool vegetarian;//是否是素食
double price;//价格 public MenuItem(string name, string description, bool 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 bool isVegetarian()
{
return vegetarian;
} }
接口Menu,它只定义了一个创建迭代器的方法,实现这个接口的类将提供各自不同的迭代器
interface Menu
{
Iterator createIterator();
}
煎饼屋菜单,实现了Menu接口
class PancakeHouseMenu : Menu
{
ArrayList menuItems;
public PancakeHouseMenu()
{
menuItems = new ArrayList();
addItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs,and toast", true, 2.99);
addItem("Regular Pancake Breakfast", "Pancakes with fired eggs,and sausage", false, 2.99);
addItem("Blueberry Pancake", "Pancakes with fresh blueberries", true, 3.49);
addItem("Waffles", "Waffles,with your choice of blueberries or strawberries", true, 3.59); }
public void addItem(string name, string description, bool vegetarian, double price)
{
MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
menuItems.Add(menuItem);
} public Iterator createIterator()
{
return new PancakeHouseIterator(menuItems);
}
//菜单的其他方法
}
餐厅菜单同样如此
class DinerMenu : Menu
{
static int Max_ITEMS = ;
int numberOfItems = ;
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,whit a side of potato salad", false, 3.29);
addItem("Hot dog", "A hot dog, with saurkraut, relish, onions, topped with cheese", false, 3.05); }
public void addItem(string name, string description, bool vegetarian, double price)
{
MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
if (numberOfItems >= Max_ITEMS)
{
Console.WriteLine("Sorry,menu is full! Can't add item to menu!");
}
else
{
menuItems[numberOfItems] = menuItem;
numberOfItems++;
}
}
public MenuItem[] getMenuItems()
{
return menuItems;
}
public Iterator createIterator()
{
return new DinerMenuIterator(menuItems);
}
//菜单的其他方法
}
接下来看看迭代器,迭代器有一个接口,定义了一些操作
interface Iterator
{
bool hasNext();
object next();
//更多的方法,例如remove...
}
不同的菜单有不同的迭代器,这是煎饼屋的
class PancakeHouseIterator:Iterator
{
ArrayList items;
int position = ;
public PancakeHouseIterator(ArrayList items)
{
this.items = items;
}
public object next()
{
MenuItem menuItem = (MenuItem)items[position];
position++;
return menuItem;
}
public bool hasNext()
{
if (position>=items.Count)
{
return false;
}
else
{
return true;
}
}
}
这是餐厅的
class DinerMenuIterator:Iterator
{
MenuItem[] items;
int position = ;
public DinerMenuIterator(MenuItem[] items)
{
this.items = items;
}
public object next()
{
MenuItem menuItem = items[position];
position++;
return menuItem;
}
public bool hasNext()
{
if (position>=items.Length||items[position]==null)
{
return false;
}
else
{
return true;
}
}
}
选择女招待可以方便的用统一的方法操作两家的菜单了
class Waitress
{
Menu pancakeHouseMenu;
Menu dinerMenu;
public Waitress(Menu pancake, Menu diner)
{
this.pancakeHouseMenu = pancake;
this.dinerMenu = diner;
}
public void printMenu()
{
Iterator pancakeIterator = pancakeHouseMenu.createIterator();
Iterator dinerIterator = dinerMenu.createIterator();
Console.WriteLine("MENU\n----\nBREAKFAT");
printMenu(pancakeIterator);
Console.WriteLine("\nLUNCH");
printMenu(dinerIterator);
}
private void printMenu(Iterator iterator)
{
while (iterator.hasNext())
{
MenuItem menuItem = (MenuItem)iterator.next();
Console.Write(menuItem.getName() + ",");
Console.WriteLine(menuItem.getPrice() + "-- ");
Console.WriteLine(" "+menuItem.getDescription());
}
}
}
迭代器的好处是将来如果有新的菜单加入,而它使用了不同的存储方法(例如hashtable),只需要让它实现迭代器接口,而不必修改女招待的操作方式。
设计模式C#实现(四)——迭代器模式的更多相关文章
- 设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型)
设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型) 1.概述 类中的面向对象编程封装应用逻辑.类,就是实例化的对象,每个单独的对象都有一个特定的身份和状态.单独的对象是一种组织代码的 ...
- Javascript设计模式之我见:迭代器模式
大家好!本文介绍迭代器模式及其在Javascript中的应用. 模式介绍 定义 提供一种方法顺序一个聚合对象中各个元素,而又不暴露该对象内部表示. 类图及说明 Iterator抽象迭代器 抽象迭代器负 ...
- 《Head first设计模式》学习笔记 – 迭代器模式
<Head first设计模式>学习笔记 – 迭代器模式 代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 爆炸性新闻:对象村餐厅和对象村煎饼屋合并了!真是个 ...
- C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
一.引言 今天我们开始讲"行为型"设计模式的第三个模式,该模式是[迭代器模式],英文名称是:Iterator Pattern.还是老套路,先从名字上来看看."迭代器模 ...
- C#设计模式之十五迭代器模式(Iterator Pattern)【行为型】
一.引言 今天我们开始讲“行为型”设计模式的第三个模式,该模式是[迭代器模式],英文名称是:Iterator Pattern.还是老套路,先从名字上来看看.“迭代器模式”我第一次看到这个名称,我的理解 ...
- C#设计模式(15)——迭代器模式
1.迭代器模式介绍 迭代器模式主要用于遍历聚合对象,将聚合对象的遍历行为分离出来,抽象为一个迭代器来负责.迭代器模式用的十分普遍,C#/JAVA等高级语言都对迭代器进行了封装用于遍历数组,集合,列表等 ...
- 设计模式17:Iterator 迭代器模式(行为型模式)
Iterator 迭代器模式(行为型模式) 动机(Motivation) 在软件构建过程中,集合对象内部结构常常变化各异.但对于这些集合对象,我们希望在不暴露其内部结构的同时,可以让外部客户代码可以透 ...
- 设计模式-(12)迭代器模式 (swift版)
一,概念 迭代器模式(Iterator Pattern)是 Java 和 .Net 编程环境中非常常用的设计模式.这种模式用于顺序访问集合对象的元素,不需要知道集合对象的底层表示.迭代器模式属于行为型 ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---迭代器模式之DinerMenu[转]
容器的主要职责有两个:存放元素和浏览元素.根据单一职责原则(SRP)要将二者分开,于是将浏览功能打包封装就有了迭代器. 用迭代器封装对动态数组的遍历: 1 2{<HeadFirst设计模式& ...
随机推荐
- 《构建之法》第8、9、10章读书笔记、读后感以及Sprint1总结
第八章:需求分析 软件需求 人们(用户)的需求五花八门,作为一个软件团队要准确而全面地获取这些需求主要有以下四个步骤: 获取和引导需求.这一步骤也被叫做“需求捕捉”.软件团队需要为用户着想,设身处地, ...
- 移动端手势库Hammer.js学习
感觉移动端原生支持的 touch.tap.swipe 几个事件好像还不够用,某些时候还会用到诸如缩放.长按等其他功能. 近日学习了一个手势库 Hammer.js,它是一个轻量级的触屏设备手势库,能识别 ...
- C#开源资源项目
一.AOP框架 Encase 是C#编写开发的为.NET平台提供的AOP框架.Encase 独特的提供了把方面(aspects)部署到运行时代码,而其它AOP框架依赖配置文件的方式.这种部署方面(as ...
- Equeue初识
详细解说: http://www.cnblogs.com/netfocus/p/3595410.html 简单代码用法: Producer 端代码用法实例 和 Customer 端代码用法示例: ht ...
- asp.net.mvc 的单文件上传和多文件上传的简单例子
首先打开vs2012,创建空的mvc4项目,名称为MVCStudy,选择基本模板
- 重新想象 Windows 8 Store Apps (58) - 微软账号
[源码下载] 重新想象 Windows 8 Store Apps (58) - 微软账号 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 微软账号 获取微软账号的用户 ...
- ASP.NET Web API 通过Authentication特性来实现身份认证
using System; using System.Collections.Generic; using System.Net.Http.Headers; using System.Security ...
- mysql内存消耗分析
最近有些生产服务器老是mysql内存不停得往上涨,开发人员和维护反馈,用了不少的临时表,问题时常线上发生,测试又一直比较难重现. 经观察mysql内存的os占用趋势,发现从8:40开始,mysql内存 ...
- Mysql进阶(二)
一.触发器 对某个表进行[增/删/改]操作的前后如果希望触发某个特定的行为时,可以使用触发器,触发器用于定制用户对表的行进行[增/删/改]前后的行为. 创建视图 # 插入前CREATE TRIGGER ...
- NYOJ 21 三个水杯
三个水杯 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有 ...