设计模式_迭代器模式

迭代器模式

介绍

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

|------------------|---------------------------------------------------------------------|
| 问题堆积在哪里 | 解决办法 |
| 不同的存储方式 统一集合查询代码 | 1 统一出一个存储方式 2 设计一个查询基类来统一查询代码 3 每个集合提供 1 统一存储方式, 2 一个查询实现(接口迭代器的实现) |

类图

代码

interface BaseIterator<T>

cs 复制代码
/// <summary>
/// 迭代器基类
/// </summary>
public interface BaseIterator<T>
{
    // 当前
    T Current();

    // 下一个
    bool MoveNext();

    // 重新开始
    void Reset();
}

IteratorPolice

cs 复制代码
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

cs 复制代码
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

cs 复制代码
public class People
{
    public string name;
    public int age;
    public bool married;
}

StudentList

cs 复制代码
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

cs 复制代码
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); 
    }
}

测试代码

cs 复制代码
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 + " ");
            }
        }
      
    }
}

结果

总结

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

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

相关推荐
_哆啦A梦11 小时前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
阿闽ooo4 天前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式
小米4964 天前
js设计模式 --- 工厂模式
设计模式
逆境不可逃4 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式
驴儿响叮当20104 天前
设计模式之状态模式
设计模式·状态模式
电子科技圈4 天前
XMOS推动智能音频等媒体处理技术从嵌入式系统转向全新边缘计算
人工智能·mcu·物联网·设计模式·音视频·边缘计算·iot
徐先生 @_@|||4 天前
安装依赖三方exe/msi的软件设计模式
设计模式
希望_睿智5 天前
实战设计模式之访问者模式
c++·设计模式·架构
茶本无香5 天前
设计模式之十六:状态模式(State Pattern)详解 -优雅地管理对象状态,告别繁琐的条件判断
java·设计模式·状态模式
驴儿响叮当20105 天前
设计模式之备忘录模式
设计模式·备忘录模式