• 设计模式_迭代器模式


    迭代器模式

    介绍

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

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

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

    类图

    代码

    interface BaseIterator

    1. ///
    2. /// 迭代器基类
    3. ///
    4. public interface BaseIterator<T>
    5. {
    6. // 当前
    7. T Current();
    8. // 下一个
    9. bool MoveNext();
    10. // 重新开始
    11. void Reset();
    12. }

    IteratorPolice 

    1. using System.Collections.Generic;
    2. public class IteratorPolice : BaseIterator<People>
    3. {
    4. // 列表
    5. List listPeople = null;
    6. // 下标
    7. int currentIndex = -1;
    8. IteratorPolice() { }
    9. public IteratorPolice(List list)
    10. {
    11. listPeople = list;
    12. }
    13. public People Current()
    14. {
    15. if (null == listPeople)
    16. return null;
    17. if (listPeople.Count < currentIndex)
    18. return null;
    19. return listPeople[currentIndex];
    20. }
    21. public bool MoveNext()
    22. {
    23. if (null == listPeople)
    24. return false;
    25. if (listPeople.Count > ++currentIndex)
    26. return true;
    27. return false;
    28. }
    29. public void Reset()
    30. {
    31. currentIndex = -1;
    32. }
    33. }

    IteratorStudent 

    1. using System.Collections.Generic;
    2. public class IteratorStudent : BaseIterator<People>
    3. {
    4. // 列表
    5. List listPeople = null;
    6. // 下标
    7. int currentIndex = -1;
    8. IteratorStudent() { }
    9. public IteratorStudent(List list)
    10. {
    11. listPeople = list;
    12. }
    13. public People Current()
    14. {
    15. if (null == listPeople)
    16. return null;
    17. if (listPeople.Count < currentIndex)
    18. return null;
    19. return listPeople[currentIndex];
    20. }
    21. public bool MoveNext()
    22. {
    23. if (null == listPeople)
    24. return false;
    25. if (listPeople.Count > ++currentIndex)
    26. return true;
    27. return false;
    28. }
    29. public void Reset()
    30. {
    31. currentIndex = -1;
    32. }
    33. }

    People

    1. public class People
    2. {
    3. public string name;
    4. public int age;
    5. public bool married;
    6. }

    StudentList

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class StudentList
    5. {
    6. private List list = new List();
    7. private People[] studentList = new People[3];
    8. public StudentList()
    9. {
    10. People p1 = new People()
    11. {
    12. name = "WH",
    13. age = 15,
    14. married = false
    15. };
    16. studentList[0] = p1;
    17. People p2 = new People()
    18. {
    19. name = "QT",
    20. age = 16,
    21. married = false
    22. };
    23. studentList[1] = p2;
    24. People p3 = new People()
    25. {
    26. name = "YY",
    27. age = 15,
    28. married = false
    29. };
    30. studentList[2] = p3;
    31. for (int i = 0; i < studentList.Length; i++)
    32. {
    33. list.Add(studentList[i]);
    34. }
    35. }
    36. public BaseIterator GetIterator()
    37. {
    38. return new IteratorStudent(list);
    39. }
    40. }

    PoliceList

    1. using System.Collections.Generic;
    2. public class PoliceList
    3. {
    4. private List list = new List();
    5. public PoliceList()
    6. {
    7. People p1 = new People()
    8. {
    9. name = "WangQiang",
    10. age = 23,
    11. married = false
    12. };
    13. list.Add(p1);
    14. People p2 = new People()
    15. {
    16. name = "ZhangQiang",
    17. age = 30,
    18. married = true
    19. };
    20. list.Add(p2);
    21. People p3 = new People()
    22. {
    23. name = "LingQiang",
    24. age = 31,
    25. married = true
    26. };
    27. list.Add(p3);
    28. }
    29. public BaseIterator GetIterator()
    30. {
    31. return new IteratorPolice(list);
    32. }
    33. }

    测试代码

    1. using UnityEngine;
    2. public class TestDDQ : MonoBehaviour
    3. {
    4. void Start()
    5. {
    6. {
    7. PoliceList pl = new PoliceList();
    8. BaseIterator iterator = pl.GetIterator();
    9. while (iterator.MoveNext())
    10. {
    11. People p1 = iterator.Current();
    12. Debug.Log("姓名:" + p1.name + " 年龄:" + p1.age + " 是否结婚:" + p1.married + " ");
    13. }
    14. }
    15. Debug.Log("------------------------------------------------------------------");
    16. {
    17. StudentList pl = new StudentList();
    18. BaseIterator iterator = pl.GetIterator();
    19. while (iterator.MoveNext())
    20. {
    21. People p1 = iterator.Current();
    22. Debug.Log("姓名:" + p1.name + " 年龄:" + p1.age + " 是否结婚:" + p1.married + " ");
    23. }
    24. }
    25. }
    26. }

    结果

    总结

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

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

  • 相关阅读:
    三相Vienna整流器电流畸变的抑制方法
    latex图片在双栏文档中横跨两栏
    金龙鱼半年报:增收不增利,控本依旧是头等大事
    networkx使用draw画图报错:TypeError: ‘_AxesStack‘ object is not callable
    linux-磁盘应用
    百数助力山西读印:印章定制行业的信息化转型深度解析
    【Azure 应用服务】本地创建Azure Function Kafka Trigger 函数和Kafka output的HTTP Trigger函数实验
    linux☞container_of
    day02_第一个Java程序
    OpenYurt环境搭建(遇到的各种坑和解决办法)
  • 原文地址:https://blog.csdn.net/qq_30926011/article/details/133864248