|
| 1 | +//------------------------------------------------------------------------------------- |
| 2 | +// IteratorExample2.cs |
| 3 | +//------------------------------------------------------------------------------------- |
| 4 | + |
| 5 | +using UnityEngine; |
| 6 | +using System.Collections; |
| 7 | +using System.Collections.Generic; |
| 8 | + |
| 9 | + |
| 10 | +/* Iterator provides uniform way to access different collections of objects |
| 11 | + * If you get an Array, ArrayList, Hashtable of objects, you pop out an iterator for each and treat them the same |
| 12 | + * |
| 13 | + * This provides a uniform way to cycle through different collections |
| 14 | + * |
| 15 | + * You can also write polymorphic code because you can refer to each collection of objects |
| 16 | + * because they'll implement the same interface |
| 17 | + * */ |
| 18 | + |
| 19 | + |
| 20 | +public class IteratorExample2 : MonoBehaviour |
| 21 | +{ |
| 22 | +void Start ( ) |
| 23 | +{ |
| 24 | + // creating the collections and adding some songs: |
| 25 | + SongsOfThe70s song70s = new SongsOfThe70s(); |
| 26 | + song70s.AddSong("song title", "song artist", 1974); |
| 27 | + song70s.AddSong("song title2", "song artist2", 1978); |
| 28 | + |
| 29 | + SongsOfThe80s song80s = new SongsOfThe80s(); |
| 30 | + song80s.AddSong("song title 80s", "song artist 80s", 1985); |
| 31 | + song80s.AddSong("song title2 80s", "song artist2 80s", 1989); |
| 32 | + |
| 33 | + // because of the iterator pattern we can loop through both types |
| 34 | + // of collections the same simple way and don't have to bother |
| 35 | + // with what type of collection the object stores: |
| 36 | + |
| 37 | + IEnumerator songsOfThe70sIterator = song70s.GetIterator(); |
| 38 | + while (songsOfThe70sIterator.MoveNext()) |
| 39 | + { |
| 40 | + SongInfo info = (SongInfo)songsOfThe70sIterator.Current; |
| 41 | + Debug.Log("Song 70s: " + info.ToString()); |
| 42 | + } |
| 43 | + |
| 44 | + IEnumerator songsOfThe80sIterator = song80s.GetIterator(); |
| 45 | + while (songsOfThe80sIterator.MoveNext()) |
| 46 | + { |
| 47 | + SongInfo info = (SongInfo)songsOfThe80sIterator.Current; |
| 48 | + Debug.Log("Song 80s: " + info.ToString()); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + // just a sample object to hold some arbitrary information for demonstration |
| 55 | + public class SongInfo |
| 56 | + { |
| 57 | + public string songName { get; protected set; } |
| 58 | + |
| 59 | + public string bandName { get; protected set; } |
| 60 | + |
| 61 | + public int yearReleased { get; protected set; } |
| 62 | + |
| 63 | + public SongInfo(string songName, string bandName, int yearReleased) |
| 64 | + { |
| 65 | + this.songName = songName; |
| 66 | + this.bandName = bandName; |
| 67 | + this.yearReleased = yearReleased; |
| 68 | + } |
| 69 | + |
| 70 | + public string ToString() |
| 71 | + { |
| 72 | + return this.songName + " - " + this.bandName + " : " + this.yearReleased.ToString(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + // Iterator Interface |
| 79 | + public interface SongIterator |
| 80 | + { |
| 81 | + IEnumerator GetIterator(); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + // These two classes implement the iterator |
| 87 | + public class SongsOfThe70s : SongIterator |
| 88 | + { |
| 89 | + // here it is a list |
| 90 | + protected List<SongInfo> bestSongs; |
| 91 | + |
| 92 | + public SongsOfThe70s() |
| 93 | + { |
| 94 | + bestSongs = new List<SongInfo>(); |
| 95 | + } |
| 96 | + |
| 97 | + public void AddSong(string name, string artist, int year) |
| 98 | + { |
| 99 | + SongInfo song = new SongInfo(name, artist, year); |
| 100 | + bestSongs.Add(song); |
| 101 | + } |
| 102 | + |
| 103 | + //about yeild return :http://www.jb51.net/article/54810.htm |
| 104 | + // heart |
| 105 | + public IEnumerator GetIterator() |
| 106 | + { |
| 107 | + foreach (SongInfo song in bestSongs) |
| 108 | + yield return song; |
| 109 | + yield break; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public class SongsOfThe80s : SongIterator |
| 114 | + { |
| 115 | + // here we have an array |
| 116 | + protected SongInfo[] bestSongs; |
| 117 | + |
| 118 | + public SongsOfThe80s() |
| 119 | + { |
| 120 | + bestSongs = new SongInfo[0]; |
| 121 | + } |
| 122 | + |
| 123 | + public void AddSong(string name, string artist, int year) |
| 124 | + { |
| 125 | + SongInfo song = new SongInfo(name, artist, year); |
| 126 | + // just for the sake of easyness of appending something we will convert the array to a list |
| 127 | + List<SongInfo> newSongs = new List<SongInfo>(bestSongs); |
| 128 | + // add a new element |
| 129 | + newSongs.Add(song); |
| 130 | + // and convert it back to an array |
| 131 | + bestSongs = newSongs.ToArray(); |
| 132 | + } |
| 133 | + |
| 134 | + // heart |
| 135 | + public IEnumerator GetIterator() |
| 136 | + { |
| 137 | + foreach (SongInfo song in bestSongs) |
| 138 | + yield return song; |
| 139 | + yield break; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | + |
0 commit comments