当前位置: 首页 > news >正文

设计模式_迭代器模式

迭代器模式

介绍

设计模式定义案例
迭代器模式行为型:关注对象与行为的分离
提供了一种统一的方式来访问多个不同的集合
两个集合:使用了不同的数据存储方式
学生 和 警察

查询显示出集合的内容 ,使用相同的代码
问题堆积在哪里解决办法
不同的存储方式

统一集合查询代码
1 统一出一个存储方式
2 设计一个查询基类来统一查询代码
3 每个集合提供
  1 统一存储方式,
  2 一个查询实现(接口迭代器的实现)

类图

代码

interface BaseIterator<T>

/// <summary>
/// 迭代器基类
/// </summary>
public interface BaseIterator<T>
{// 当前T Current();// 下一个bool MoveNext();// 重新开始void Reset();
}

IteratorPolice 

using System.Collections.Generic;public class IteratorPolice : BaseIterator<People>
{// 列表List<People> listPeople = null;// 下标int currentIndex = -1;  IteratorPolice() { }public IteratorPolice(List<People> list){listPeople = list;}public People Current(){if (null == listPeople)return null;if (listPeople.Count < currentIndex)return null;return listPeople[currentIndex];}public bool MoveNext(){if (null == listPeople)return false;if (listPeople.Count > ++currentIndex)return true;return false;}public void Reset(){currentIndex = -1;}
}

IteratorStudent 


using System.Collections.Generic;public class IteratorStudent : BaseIterator<People>
{// 列表List<People> listPeople = null;// 下标int currentIndex = -1;IteratorStudent() { }public IteratorStudent(List<People> list){listPeople = list;}public People Current(){if (null == listPeople)return null;if (listPeople.Count < currentIndex)return null;return listPeople[currentIndex];}public bool MoveNext(){if (null == listPeople)return false;if (listPeople.Count > ++currentIndex)return true;return false;}public void Reset(){currentIndex = -1;}}

People


public class People
{public string name;public int age;public bool married;
}

StudentList

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class StudentList
{private List<People> list = new List<People>();private People[] studentList = new People[3];public StudentList(){People p1 = new People(){name = "WH",age = 15,married = false};studentList[0] = p1;People p2 = new People(){name = "QT",age = 16,married = false};studentList[1] = p2;People p3 = new People(){name = "YY",age = 15,married = false};studentList[2] = p3;for (int i = 0; i < studentList.Length; i++){list.Add(studentList[i]);}}public BaseIterator<People> GetIterator(){return new IteratorStudent(list);}
}

PoliceList


using System.Collections.Generic;public class PoliceList
{private List<People> list = new List<People>();public PoliceList(){People p1 = new People(){name = "WangQiang",age = 23,married = false};list.Add(p1);People p2 = new People(){name = "ZhangQiang",age = 30,married = true};list.Add(p2);People p3 = new People(){name = "LingQiang",age = 31,married = true};list.Add(p3);}public BaseIterator<People> GetIterator(){return new IteratorPolice(list); }
}

测试代码

using UnityEngine;public class TestDDQ : MonoBehaviour
{void Start(){{PoliceList pl = new PoliceList();BaseIterator<People> iterator = pl.GetIterator();while (iterator.MoveNext()){People p1 = iterator.Current();Debug.Log("姓名:" + p1.name + "   年龄:" + p1.age + "   是否结婚:" + p1.married + " ");}}Debug.Log("------------------------------------------------------------------");{StudentList pl = new StudentList();BaseIterator<People> iterator = pl.GetIterator();while (iterator.MoveNext()){People p1 = iterator.Current();Debug.Log("姓名:" + p1.name + "   年龄:" + p1.age + "   是否结婚:" + p1.married + " ");}}}
}

结果

总结

迭代器模式统一集合查询代码,以这个为目标进现优化总结出的一个经验。

还是为了更深刻的理解设计原理和优化手段。

http://www.lryc.cn/news/195071.html

相关文章:

  • 【数据结构】:栈的实现
  • 微前端一:技术选型
  • FPGA project : flash_continue_write
  • 论文阅读:Rethinking Range View Representation for LiDAR Segmentation
  • 本地配置免费的https咋做?
  • 微信小程序框架---详细教程
  • 【LeetCode刷题(数组and排序)】:存在重复元素
  • 半导体产业链解析:晶圆厂、无晶圆厂与代工厂的比较与作用
  • Apipost一键压测已支持导入CSV文件
  • RabbitMQ的5种模式——再探RabbitMQ的模式,简单、工作,发布订阅(广播),路由、主题 页面分析
  • 初识华为云数据库GaussDB for openGauss
  • 深圳寄包裹到德国
  • 系统架构师备考倒计时22天(每日知识点)Redis篇
  • 现有库存(on-hand inventory),库存水平(inventory level),库存位置(inventory position)
  • 智慧空开让用电更安全、管理更智能——电脑APP远程控制开合闸
  • PyTorch 中张量运算广播
  • Blender:使用立方体制作动漫头像
  • 【ppt技巧】ppt里的图片如何提取出来?
  • Python学习基础笔记七十三——调试程序
  • BOSHIDA DC电源模块关于电容器的电解液位置
  • 如何实现 Es 全文检索、高亮文本略缩处理(封装工具接口极致解耦)
  • C++多线程编程(第四章 案例1,C++11和C++17 多核并行计算样例)
  • 获取远程仓库的信息和远程分支的信息
  • QT学习day1
  • unity面试八股文 - 框架设计与资源管理
  • 智能网关IOT 2050采集应用
  • iOS代码混淆-从入门到放弃
  • 基于Eigen的位姿转换
  • Jmeter之Bean shell使用详解
  • TCP/IP(八)TCP的连接管理(五)四次握手