Last active
June 11, 2016 02:59
-
-
Save ShirakawaYoshimaru/06a9753a1baece5ded1dd85800dbca50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
public class TestScript : MonoBehaviour | |
{ | |
void Start () | |
{ | |
var bookShelf = new Iterator1.BookShelf (); | |
bookShelf.Add (new Book ("111")); | |
bookShelf.Add (new Book ("222")); | |
bookShelf.Add (new Book ("333")); | |
var iterator = bookShelf.Iterator (); | |
while (iterator.HasNext ()) { | |
var book = iterator.Next (); | |
Debug.Log (book.GetName ()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Aggeregate<T> | |
{ | |
Iterator<T> Iterator (); | |
} | |
public interface Iterator<T> | |
{ | |
bool HasNext (); | |
T Next (); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
namespace Iterator1 | |
{ | |
public class BookShelf : Aggeregate<Book> | |
{ | |
List<Book> books = new List<Book> (); | |
public Book GetAt (int index) | |
{ | |
return books [index]; | |
} | |
public void Add (Book data) | |
{ | |
books.Add (data); | |
} | |
public void RemoveAt (int index) | |
{ | |
books.RemoveAt (index); | |
} | |
public int GetCount () | |
{ | |
return books.Count; | |
} | |
public Iterator<Book> Iterator () | |
{ | |
return new BookIterator (this); | |
} | |
} | |
public class BookIterator : Iterator<Book> | |
{ | |
private BookShelf bookShelf; | |
private int index; | |
public BookIterator (BookShelf bookShelf) | |
{ | |
this.bookShelf = bookShelf; | |
} | |
public bool HasNext () | |
{ | |
return index < bookShelf.GetCount (); | |
} | |
public Book Next () | |
{ | |
var item = bookShelf.GetAt (index); | |
index++; | |
return item; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment