设计模式 | 定义 | 案例 |
迭代器模式 | 行为型:关注对象与行为的分离 提供了一种统一的方式来访问多个不同的集合 | 两个集合:使用了不同的数据存储方式 学生 和 警察 查询显示出集合的内容 ,使用相同的代码 |
问题堆积在哪里 | 解决办法 |
不同的存储方式 统一集合查询代码 | 1 统一出一个存储方式 2 设计一个查询基类来统一查询代码 3 每个集合提供 1 统一存储方式, 2 一个查询实现(接口迭代器的实现) |
- ///
- /// 迭代器基类
- ///
- public interface BaseIterator<T>
- {
- // 当前
- T Current();
-
- // 下一个
- bool MoveNext();
-
- // 重新开始
- void Reset();
- }
- using System.Collections.Generic;
-
- public class IteratorPolice : BaseIterator<People>
- {
- // 列表
- List
listPeople = null; - // 下标
- int currentIndex = -1;
-
- IteratorPolice() { }
- public IteratorPolice(List
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;
- }
- }
-
- using System.Collections.Generic;
-
- public class IteratorStudent : BaseIterator<People>
- {
- // 列表
- List
listPeople = null; - // 下标
- int currentIndex = -1;
-
- IteratorStudent() { }
- public IteratorStudent(List
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;
- }
-
- }
-
- public class People
- {
- public string name;
- public int age;
- public bool married;
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class StudentList
- {
- private List
list = new List(); -
- 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
GetIterator() - {
- return new IteratorStudent(list);
- }
- }
-
- using System.Collections.Generic;
-
- public class PoliceList
- {
- private List
list = new List(); -
- 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
GetIterator() - {
- return new IteratorPolice(list);
- }
- }
- using UnityEngine;
-
- public class TestDDQ : MonoBehaviour
- {
- void Start()
- {
- {
- PoliceList pl = new PoliceList();
- BaseIterator
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
iterator = pl.GetIterator(); -
- while (iterator.MoveNext())
- {
- People p1 = iterator.Current();
- Debug.Log("姓名:" + p1.name + " 年龄:" + p1.age + " 是否结婚:" + p1.married + " ");
- }
- }
-
- }
- }
迭代器模式统一集合查询代码,以这个为目标进现优化总结出的一个经验。
还是为了更深刻的理解设计原理和优化手段。