## 一、迭代器模式

#### 1.2迭代器例子

public class MenuItem
{
private string name;
private string description;
private bool vegetarin;
private double price;

public MenuItem(string name, string description, bool vegetarin, double price)
{
this.name = name;
this.description = description;
this.vegetarin = vegetarin;
this.price = price;
}

public string GetName()
{
return name;
}

public double GetPrice()
{
return price;
}

public bool IsVegetarian()
{
return vegetarin;
}

public string GetDescription()
{
return description;
}
}

public class BreakfastMenu
{
{
}

public void AddItem(string name, string description, bool vegetarian, double price)
{
}

{
}
}

{
static readonly int Max_ITEMS = 6;
int numberOfItems = 0;

{
}

public void AddItem(string name, string description, bool vegetarian, double price)
{
if (numberOfItems>Max_ITEMS)
{
Console.WriteLine("菜单已满");
}
else
{
numberOfItems++;
}
}
{
}
}

BreakfastMenu breakfastMenu = new BreakfastMenu();

for (int i = 0; i < breakfastItems.Count; i++)
{
}

for (int j = 0; j < lunchItems.Length; j++)
{
if (lunchItem!=null)
{
Console.WriteLine(lunchItem.GetName() + " " + lunchItem.GetPrice().ToString() + " " + lunchItem.GetDescription().ToString());
}
}

Console.ReadKey();

public interface Iterator
{
/// <summary>
/// 用来判断下一个元素是否为空
/// </summary>
/// <returns></returns>
bool HasNext();

/// <summary>
/// 用来获取当前元素
/// </summary>
/// <returns></returns>
object Next();
}

while (iterator.HasNext())
{

}

public class BreakfastIterator : Iterator
{
private ArrayList items;
private int position;
public BreakfastIterator(ArrayList arrayList)
{
items = arrayList;
}
public bool HasNext()
{
if (position>items.Count||items[position]==null)
{
return false;
}
else
{
return true;
}
}

public object Next()
{
position = position + 1;
}
}

public class DinnerIterator : Iterator
{
private int position = 0;

{
this.items = items;
}
public bool HasNext()
{
if (position > items.Length || items[position] == null)
{
return false;
}
else
{
return true;
}
}

public object Next()
{
position = position + 1;
}
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IteratorPattern
{
class Program
{
static void Main(string[] args)
{

//for (int i = 0; i < breakfastItems.Count; i++)
//{
//}

//for (int j = 0; j < lunchItems.Length; j++)
//{
//    if (lunchItem != null)
//    {
//        Console.WriteLine(lunchItem.GetName() + " " + lunchItem.GetPrice().ToString() + " " + lunchItem.GetDescription().ToString());
//    }
//}

Print(breakfastIterator);
Print(dinnerIterator);

}

static void Print(Iterator iterator)
{
while (iterator.HasNext())
{
}
}
}

{
private string name;
private string description;
private bool vegetarin;
private double price;

public MenuItem(string name, string description, bool vegetarin, double price)
{
this.name = name;
this.description = description;
this.vegetarin = vegetarin;
this.price = price;
}

public string GetName()
{
return name;
}

public double GetPrice()
{
return price;
}

public bool IsVegetarian()
{
return vegetarin;
}

public string GetDescription()
{
return description;
}

}

{

{
}

public void AddItem(string name, string description, bool vegetarian, double price)
{
}

{
}

public Iterator CreateIterator()
{
}

}

{
static readonly int Max_ITEMS = 6;
int numberOfItems = 0;

{
}

public void AddItem(string name, string description, bool vegetarian, double price)
{
if (numberOfItems>Max_ITEMS)
{
Console.WriteLine("菜单已满");
}
else
{
numberOfItems++;
}
}
{
}

public Iterator CreateIterator()
{
}
}

public interface Iterator
{
/// <summary>
/// 用来判断下一个元素是否为空
/// </summary>
/// <returns></returns>
bool HasNext();

/// <summary>
/// 用来获取当前元素
/// </summary>
/// <returns></returns>
object Next();
}

public class BreakfastIterator : Iterator
{
private ArrayList items;
private int position;
public BreakfastIterator(ArrayList arrayList)
{
items = arrayList;
}
public bool HasNext()
{
if (position>=items.Count||items[position]==null)
{
return false;
}
else
{
return true;
}
}

public object Next()
{
position = position + 1;
}
}

public class DinnerIterator : Iterator
{
private int position = 0;

{
this.items = items;
}
public bool HasNext()
{
if (position > items.Length || items[position] == null)
{
return false;
}
else
{
return true;
}
}

public object Next()
{
position = position + 1;
}
}

{
Iterator CreateIterator();
}

}

### 二、组合模式

#### 2.2组合模式例子

class Program
{
static void Main(string[] args)
{
//定义早餐====================================================================

//定义午餐====================================================================

//定义三餐====================================================================
food.Print();
}

}

/// <summary>
/// 菜单组件
/// </summary>
{

public abstract string GetDescription();

public abstract string GetName();

public abstract double GetPrice();

public abstract bool IsVegetarian();

public abstract void Print();
}

{
private string name;
private string description;
private bool vegetarin;
private double price;

public MenuItem(string name, string description, bool vegetarin, double price)
{
this.name = name;
this.description = description;
this.vegetarin = vegetarin;
this.price = price;
}

public override  string GetName()
{
return name;
}

public override double GetPrice()
{
return price;
}

public override bool IsVegetarian()
{
return vegetarin;
}

public override string GetDescription()
{
return description;
}

public override void Print()
{
Console.Write(""+GetName()+",");
if (IsVegetarian())
{
Console.Write("（素） ,");
}
Console.Write(GetPrice()+"￥,");
Console.WriteLine(GetDescription()+"。");
}

{
throw new NotImplementedException();
}

{
throw new NotImplementedException();
}

{
throw new NotImplementedException();
}
}

{
private string name;
private string description;

{
this.name = name;
this.description = description;
}

{
return;
}

{
}

public override string GetName()
{
return name;
}

public override string GetDescription()
{
return description;
}

public override void Print()
{

Console.Write("--"+GetName());
Console.WriteLine("," + GetDescription());

while (enumerator.MoveNext())
{
}
}
{
throw new NotImplementedException();
}
public override double GetPrice()
{
throw new NotImplementedException();
}

public override bool IsVegetarian()
{
throw new NotImplementedException();
}
}

posted @ 2013-11-17 13:11  haiziguo  阅读(6968)  评论(0编辑  收藏