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

Unity实现设计模式——迭代器模式

Unity实现设计模式——迭代器模式

迭代器模式是一种行为型设计模式,它提供了一种统一的方式来访问集合对象中的元素,而不是暴露集合内部的表示方式。简单地说,就是将遍历集合的责任封装到一个单独的对象中,我们可以按照特定的方式访问集合中的元素。

抽象迭代器(Iterator):定义了遍历聚合对象所需的方法,包括hashNext()和next()方法等,用于遍历聚合对象中的元素。
具体迭代器(Concrete Iterator):它是实现迭代器接口的具体实现类,负责具体的遍历逻辑。它保存了当前遍历的位置信息,并可以根据需要向前或向后遍历集合元素。
抽象聚合器(Aggregate): 一般是一个接口,提供一个iterator()方法,例如java中的Collection接口,List接口,Set接口等。
具体聚合器(ConcreteAggregate):就是抽象容器的具体实现类,比如List接口的有序列表实现ArrayList,List接口的链表实现LinkList,Set接口的哈希列表的实现HashSet等。

简单来说就是容器的种类我们不需要知道是什么(List,HasSet等)我们只要获取到迭代器就可以进行遍历。
下面使用两个例子来说明

1.抽象的例子

(一)Aggregate

抽象聚合器

    abstract class Aggregate{public abstract Iterator CreateIterator();}

(二)ConcreteAggregate

具体聚合器

    class ConcreteAggregate : Aggregate{private ArrayList _items = new ArrayList();public override Iterator CreateIterator(){return new ConcreteIterator(this);}// Gets item countpublic int Count{get { return _items.Count; }}// Indexerpublic object this[int index]{get { return _items[index]; }set { _items.Insert(index, value); }}}

(三)Iterator

抽象迭代器

    abstract class Iterator{public abstract object First();public abstract object Next();public abstract bool IsDone();public abstract object CurrentItem();}

(四)ConcreteIterator

具体迭代器

    class ConcreteIterator : Iterator{private ConcreteAggregate _aggregate;private int _current = 0;// Constructorpublic ConcreteIterator(ConcreteAggregate aggregate){this._aggregate = aggregate;}// Gets first iteration itempublic override object First(){return _aggregate[0];}// Gets next iteration itempublic override object Next(){object ret = null;if (_current < _aggregate.Count - 1){ret = _aggregate[++_current];}return ret;}// Gets current iteration itempublic override object CurrentItem(){return _aggregate[_current];}// Gets whether iterations are completepublic override bool IsDone(){return _current >= _aggregate.Count;}}

(五)测试

public class IteratorStructure : MonoBehaviour
{void Start ( ){ConcreteAggregate a = new ConcreteAggregate();a[0] = "Item A";a[1] = "Item B";a[2] = "Item C";a[3] = "Item D";// Create Iterator and provide aggregateIterator i = a.CreateIterator();Debug.Log("Iterating over collection:");object item = i.First();while (item != null){Debug.Log(item);item = i.Next();}}
}

2.现实例子

这里使用一个不同年代的歌曲集来进行演示

(一)SongInfo

歌曲信息

    public class SongInfo{public string songName { get; protected set; }public string bandName { get; protected set; }public int yearReleased { get; protected set; }public SongInfo(string songName, string bandName, int yearReleased){this.songName = songName;this.bandName = bandName;this.yearReleased = yearReleased;}public string ToStringEx(){return this.songName + " - " + this.bandName + " : " + this.yearReleased.ToString();}}

(二)SongIterator

抽象迭代器

    public interface SongIterator{IEnumerator GetIterator();}

(三)SongsOfThe70s

70年代歌曲,既是迭代器又是聚合器

    public class SongsOfThe70s : SongIterator{// here it is a listprotected List<SongInfo> bestSongs;public SongsOfThe70s(){bestSongs = new List<SongInfo>();}public void AddSong(string name, string artist, int year){SongInfo song = new SongInfo(name, artist, year);bestSongs.Add(song);}//about yeild return :http://www.jb51.net/article/54810.htm// heartpublic IEnumerator GetIterator(){foreach (SongInfo song in bestSongs)yield return song;yield break;}}

(四)SongsOfThe80s

80年代歌曲,既是迭代器又是聚合器

    public class SongsOfThe80s : SongIterator{// here we have an arrayprotected SongInfo[] bestSongs;public SongsOfThe80s(){bestSongs = new SongInfo[0];}public void AddSong(string name, string artist, int year){SongInfo song = new SongInfo(name, artist, year);// just for the sake of easyness of appending something we will convert the array to a listList<SongInfo> newSongs = new List<SongInfo>(bestSongs);// add a new elementnewSongs.Add(song);// and convert it back to an arraybestSongs = newSongs.ToArray();}// heartpublic IEnumerator GetIterator(){foreach (SongInfo song in bestSongs)yield return song;yield break;}}

(五)测试

    void Start(){// creating the collections and adding some songs:SongsOfThe70s song70s = new SongsOfThe70s();song70s.AddSong("song title", "song artist", 1974);song70s.AddSong("song title2", "song artist2", 1978);SongsOfThe80s song80s = new SongsOfThe80s();song80s.AddSong("song title 80s", "song artist 80s", 1985);song80s.AddSong("song title2 80s", "song artist2 80s", 1989);// because of the iterator pattern we can loop through both types// of collections the same simple way and don't have to bother// with what type of collection the object stores:IEnumerator songsOfThe70sIterator = song70s.GetIterator();while (songsOfThe70sIterator.MoveNext()){SongInfo info = (SongInfo)songsOfThe70sIterator.Current;Debug.Log("Song 70s: " + info.ToStringEx());}IEnumerator songsOfThe80sIterator = song80s.GetIterator();while (songsOfThe80sIterator.MoveNext()){SongInfo info = (SongInfo)songsOfThe80sIterator.Current;Debug.Log("Song 80s: " + info.ToStringEx());}}
http://www.lryc.cn/news/184387.html

相关文章:

  • 【数据结构与算法】之“堆”介绍
  • ncnn Fatal signal 11 (SIGSEGV) 使用GPU加速崩溃
  • 计算机考研 | 2018年 | 计算机组成原理真题
  • 用Configuration注解的方式写一个java过滤器的详细实例?
  • 基于Springboot实现旧物置换网站平台演示【项目源码+论文说明】分享
  • 想要精通算法和SQL的成长之路 - 存在重复元素
  • 使用华为eNSP组网试验⑸-访问控制
  • iPhone苹果手机闹钟智能跳过节假日怎么设置?
  • TenDB Cluster 简介
  • 【刷题笔记10.6】LeetCode:翻转二叉树
  • 【高阶数据结构】图详解第一篇:图的基本概念及其存储结构(邻接矩阵和邻接表)
  • IPV4跟IPV6的区别
  • 利用fitnesse实现api接口自动化测试
  • 【LeetCode】1154.一年中的第几天
  • 4.物联网射频识别,RFID开发【智能门禁项目】
  • CompletableFuture 和 Future 的选择,以及CompletableFuture的用法
  • 美国第三大财产和意外险公司利宝保险集团利用 OpenText EnCase 取证收集技术控制法律风险和成本
  • 打包报错JavaScript heap out of memory
  • Android Camera FW 里的requestId和frameId
  • 代理IP与Socks5代理在技术世界的多元应用
  • 计算机专业毕业设计项目推荐12-志愿者管理系统(Spring+Js+Mysql)
  • 苹果文件传到mac电脑用什么软件?
  • 深入理解Docker:简化部署与管理的利器
  • 软考对找工作有用吗?
  • Android系统启动之init进程启动+Zygote进程启动分析
  • 微信这样的加人方式,既安全又解放双手
  • CVE-2023-5129:libwebp开源库10分漏洞
  • 从零开始的C++(六)
  • leetcode 518. 零钱兑换 II、377. 组合总和 Ⅳ
  • 【网络安全 --- kali2022安装】kali2022 超详细的安装教程(提供镜像)