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

兰州企业建设网站小型企业网站的设计与实现

兰州企业建设网站,小型企业网站的设计与实现,软件公司网站模板,企业类网站源码Unity实现设计模式——迭代器模式 迭代器模式是一种行为型设计模式#xff0c;它提供了一种统一的方式来访问集合对象中的元素#xff0c;而不是暴露集合内部的表示方式。简单地说#xff0c;就是将遍历集合的责任封装到一个单独的对象中#xff0c;我们可以按照特定的方式…Unity实现设计模式——迭代器模式 迭代器模式是一种行为型设计模式它提供了一种统一的方式来访问集合对象中的元素而不是暴露集合内部的表示方式。简单地说就是将遍历集合的责任封装到一个单独的对象中我们可以按照特定的方式访问集合中的元素。 抽象迭代器Iterator定义了遍历聚合对象所需的方法包括hashNext()和next()方法等用于遍历聚合对象中的元素。 具体迭代器Concrete Iterator它是实现迭代器接口的具体实现类负责具体的遍历逻辑。它保存了当前遍历的位置信息并可以根据需要向前或向后遍历集合元素。 抽象聚合器Aggregate 一般是一个接口提供一个iterator()方法例如java中的Collection接口List接口Set接口等。 具体聚合器ConcreteAggregate就是抽象容器的具体实现类比如List接口的有序列表实现ArrayListList接口的链表实现LinkListSet接口的哈希列表的实现HashSet等。 简单来说就是容器的种类我们不需要知道是什么ListHasSet等我们只要获取到迭代器就可以进行遍历。 下面使用两个例子来说明 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 ListSongInfo bestSongs;public SongsOfThe70s(){bestSongs new ListSongInfo();}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 listListSongInfo newSongs new ListSongInfo(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 dont 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.ho-use.cn/article/10818153.html

相关文章:

  • 网站每月流量哪个网站可以做担保交易平台
  • 网站后台管理系统cms做淘宝客网站有什么服务器
  • 免费网站模板的制作方法html页面生成器
  • 如何利用网站新闻做推广免费网络推广怎么做
  • 网站建设的500字小结门头沟新乡网站建设
  • 南昌网站排名优化软件查看网站的 cms
  • 网站模板提供源码深圳手机移动网站开发
  • 群晖nas做网站性能wordpress固定链接中文
  • 襄阳哪里做网站友情链接怎么添加
  • 凡科建站免费版可以做什么正规的推文平台
  • wordpress 安装中文字体windows优化大师有什么功能
  • 学校网站设计实验报告互动营销的案例有哪些
  • 石家庄做网站的有哪些公司30天网站建设 视频教程
  • 网站后缀类型利尔化学股票股吧
  • 淘宝网站建设可以申请赔款word模板网
  • 电子商务网站建设系统特点软件ui设计教程
  • 网站副标题的作用怎么做根优酷差不多的网站
  • 试列出网站开发建设的步骤铜川有哪些需要网页设计师的公司
  • 建设电影网站需要多少钱营销型网站建设的优势有哪些
  • 济南做网站的公司s上海网站建设
  • 设计在线设计网站企业对企业的电子商务网站有哪些
  • 网站首页面设计代码国内网站 专做国外视频
  • 个人婚礼网站模板宣威网站建设公司
  • python做网站优势沈阳做网站的公司
  • 怎么将网站权重提上去微信导航网站怎么做
  • 网站建设厘金手指排名二一河南海华工程建设监理公司网站
  • 网站建设除凡科外还有哪些开源企业建站系统哪个好
  • 四川网站建设 招标网络营销论文5000字
  • 社交平台运营是做什么的河南郑州网站推广优化外包
  • 呼和浩特网站建设公司快站淘客