关于迭代器模式,我们所需要知道的第一件事情就是它依赖于一个名为迭代器的接口。一旦我们有了这个接口,就可以为各种那个对象集合实现迭代器:数组、列表、散列表。
项目实例我们就拿用户角色模块来举例
背景
1.某用户作为应用管理员获取一组关于管理角色的arraylist权限组
2.该用户作为应用开发者获取一组关于开发角色的数组形式的权限组
...
/// <summary>
/// 迭代器接口
/// </summary>
public interface RoleIterator
{
//返回一个布尔值,让客户知道是否还有更多的元素
bool hasnext();
//返回下一个元素
Object next();
}
/// <summary>
/// 为管理者服务的迭代器
/// </summary>
public class ManagerRoleIterator : RoleIterator
{
ArrayList items;
int position = 0;
public ManagerRoleIterator(ArrayList RoleItems)
{
this.items = RoleItems;
}
public bool hasnext()
{
if (position >= items.Count || items[position] == null)
{
return false;
}
return true;
}
public Object next()
{
Object ri = items[position];
position++;
return ri;
}
}
/// <summary>
/// 管理者
/// </summary>
public class ManagerRole
{
ArrayList items;
public RoleIterator createIterator()
{
return new ManagerRoleIterator(items);
}
}
/// <summary>
/// 为开发者服务的迭代器
/// </summary>
public class DevelopRoleIterator : RoleIterator
{
RoleItem[] items;
int position = 0;
public DevelopRoleIterator(RoleItem[] RoleItems)
{
this.items = RoleItems;
}
public bool hasnext()
{
if (position >= items.Length || items[position] == null)
{
return false;
}
return true;
}
public Object next()
{
RoleItem ri = items[position];
position++;
return ri;
}
}
/// <summary>
/// 开发者
/// </summary>
public class DevelopRole
{
static int MAX_ITEMS = 6;
int numberofitems = 0;
RoleItem[] items;
public RoleIterator createIterator()
{
return new DevelopRoleIterator(items);
}
}
/// <summary>
/// 用户
/// </summary>
public class UserRole
{
ManagerRole mrole;
DevelopRole drole;
public UserRole(ManagerRole mrole, DevelopRole drole)
{
this.mrole = mrole;
this.drole = drole;
}
public List<Object> getRole()
{
List<Object> lo = new List<object>();
RoleIterator mi = mrole.createIterator();
RoleIterator di = drole.createIterator();
lo.AddRange(getRole(mi));
lo.AddRange(getRole(di));
return lo;
}
public List<Object> getRole(RoleIterator ri)
{
List<Object> li = new List<object>();
while (ri.hasnext())
{
RoleItem ritem = (RoleItem)ri.next();
li.Add(ritem);
}
return li;
}
}
/// 迭代器接口
/// </summary>
public interface RoleIterator
{
//返回一个布尔值,让客户知道是否还有更多的元素
bool hasnext();
//返回下一个元素
Object next();
}
/// <summary>
/// 为管理者服务的迭代器
/// </summary>
public class ManagerRoleIterator : RoleIterator
{
ArrayList items;
int position = 0;
public ManagerRoleIterator(ArrayList RoleItems)
{
this.items = RoleItems;
}
public bool hasnext()
{
if (position >= items.Count || items[position] == null)
{
return false;
}
return true;
}
public Object next()
{
Object ri = items[position];
position++;
return ri;
}
}
/// <summary>
/// 管理者
/// </summary>
public class ManagerRole
{
ArrayList items;
public RoleIterator createIterator()
{
return new ManagerRoleIterator(items);
}
}
/// <summary>
/// 为开发者服务的迭代器
/// </summary>
public class DevelopRoleIterator : RoleIterator
{
RoleItem[] items;
int position = 0;
public DevelopRoleIterator(RoleItem[] RoleItems)
{
this.items = RoleItems;
}
public bool hasnext()
{
if (position >= items.Length || items[position] == null)
{
return false;
}
return true;
}
public Object next()
{
RoleItem ri = items[position];
position++;
return ri;
}
}
/// <summary>
/// 开发者
/// </summary>
public class DevelopRole
{
static int MAX_ITEMS = 6;
int numberofitems = 0;
RoleItem[] items;
public RoleIterator createIterator()
{
return new DevelopRoleIterator(items);
}
}
/// <summary>
/// 用户
/// </summary>
public class UserRole
{
ManagerRole mrole;
DevelopRole drole;
public UserRole(ManagerRole mrole, DevelopRole drole)
{
this.mrole = mrole;
this.drole = drole;
}
public List<Object> getRole()
{
List<Object> lo = new List<object>();
RoleIterator mi = mrole.createIterator();
RoleIterator di = drole.createIterator();
lo.AddRange(getRole(mi));
lo.AddRange(getRole(di));
return lo;
}
public List<Object> getRole(RoleIterator ri)
{
List<Object> li = new List<object>();
while (ri.hasnext())
{
RoleItem ritem = (RoleItem)ri.next();
li.Add(ritem);
}
return li;
}
}
总结:迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。把访问的任务放在迭代器上,而不是聚合上。这样简化了聚合的接口和实现,也让责任各得其所。