迭代器模式:提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。

  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#实现(四)——迭代器模式的更多相关文章

  1. 设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型)

      设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型) 1.概述 类中的面向对象编程封装应用逻辑.类,就是实例化的对象,每个单独的对象都有一个特定的身份和状态.单独的对象是一种组织代码的 ...

  2. Javascript设计模式之我见:迭代器模式

    大家好!本文介绍迭代器模式及其在Javascript中的应用. 模式介绍 定义 提供一种方法顺序一个聚合对象中各个元素,而又不暴露该对象内部表示. 类图及说明 Iterator抽象迭代器 抽象迭代器负 ...

  3. 《Head first设计模式》学习笔记 – 迭代器模式

    <Head first设计模式>学习笔记 – 迭代器模式 代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 爆炸性新闻:对象村餐厅和对象村煎饼屋合并了!真是个 ...

  4. C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】

    一.引言   今天我们开始讲"行为型"设计模式的第三个模式,该模式是[迭代器模式],英文名称是:Iterator Pattern.还是老套路,先从名字上来看看."迭代器模 ...

  5. C#设计模式之十五迭代器模式(Iterator Pattern)【行为型】

    一.引言 今天我们开始讲“行为型”设计模式的第三个模式,该模式是[迭代器模式],英文名称是:Iterator Pattern.还是老套路,先从名字上来看看.“迭代器模式”我第一次看到这个名称,我的理解 ...

  6. C#设计模式(15)——迭代器模式

    1.迭代器模式介绍 迭代器模式主要用于遍历聚合对象,将聚合对象的遍历行为分离出来,抽象为一个迭代器来负责.迭代器模式用的十分普遍,C#/JAVA等高级语言都对迭代器进行了封装用于遍历数组,集合,列表等 ...

  7. 设计模式17:Iterator 迭代器模式(行为型模式)

    Iterator 迭代器模式(行为型模式) 动机(Motivation) 在软件构建过程中,集合对象内部结构常常变化各异.但对于这些集合对象,我们希望在不暴露其内部结构的同时,可以让外部客户代码可以透 ...

  8. 设计模式-(12)迭代器模式 (swift版)

    一,概念 迭代器模式(Iterator Pattern)是 Java 和 .Net 编程环境中非常常用的设计模式.这种模式用于顺序访问集合对象的元素,不需要知道集合对象的底层表示.迭代器模式属于行为型 ...

  9. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---迭代器模式之DinerMenu[转]

    容器的主要职责有两个:存放元素和浏览元素.根据单一职责原则(SRP)要将二者分开,于是将浏览功能打包封装就有了迭代器. 用迭代器封装对动态数组的遍历:  1  2{<HeadFirst设计模式& ...

随机推荐

  1. [Solution] 一步一步WCF(2) 终结点Endpoint

    繁忙的一天又一天,不管其他,先继续WCF吧. Endpoint包含地址,绑定,契约三要素.WCF作为一个Windows平台下最大的通信框架.通过终结点承载了所有通信功能.所以终结点的作用将非常重要. ...

  2. [JS] JavaScript由浅入深(2) 进阶

    本节,将围绕以下几点来讲. 知识点:多线程.作用域.闭包.this 先顶后看 1.多线程 在不支持H5的浏览器中.使用Concurrent.Thread.js. 在支持H5中,使用WebWork. 在 ...

  3. sqlserver工作日常使用sql--持续完善中

    select STUFF('232',1,1,'')结果为32,从第一个字符开始去掉一个字符,及去掉 select CONCAT('-','asd')结果为-asd,连接两个字符串 select co ...

  4. 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解

    [源码下载] 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Tile ...

  5. JDK动态代理的实现及原理

    Proxy.newProxyInstance(classloader,Class,invocationHandler) 调用getProxyClass0(loader, interfaces)生成代理 ...

  6. #define lowbit(x) ((x)&(-x))原理详解

    #define lowbit(x) ((x)&(-x)) 也可以写成如下形式: int Lowbit(x) { return x&(-x); } 例如: 1> x = 1: 十进 ...

  7. ahjesus让nodejs支持dotjs模板

    经过几天的实验加搜索,终于知道一个中间件可以解决这个问题了 npm install consolidate consolidate传送门 传送门2使用说明传送门快照:ahjesus Since doT ...

  8. C# 循环语句 for循环(嵌套 while 穷举 迭代)

    for循环的嵌套类似于if else 事例: 打印矩阵,外循环对应行,内循环对应列 for (int k = 1; k <= 5; k++) { for (int i = 1; i <= ...

  9. django性能优化

    1. 内存.内存,还是加内存 2. 使用单独的静态文件服务器 3. 关闭KeepAlive(如果服务器不提供静态文件服务,如:大文件下载) 4. 使用memcached 5. 使用select_rel ...

  10. 复杂表格的树形结构的添加删除行div实现

    公司倒闭,换了工作,无奈选择了做外包这个差事,大公司进不去,小公司工资太低,可能也只能如此了.但无奈之举,亦不可浪费时间,多多磨练自己吧! 众所周知,做外包项目,其实就是做一些大公司的内部系统,多以管 ...