Skip to content

Instantly share code, notes, and snippets.

@ShirakawaYoshimaru
Last active June 11, 2016 02:59
Show Gist options
  • Save ShirakawaYoshimaru/06a9753a1baece5ded1dd85800dbca50 to your computer and use it in GitHub Desktop.
Save ShirakawaYoshimaru/06a9753a1baece5ded1dd85800dbca50 to your computer and use it in GitHub Desktop.
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 ());
}
}
}
public interface Aggeregate<T>
{
Iterator<T> Iterator ();
}
public interface Iterator<T>
{
bool HasNext ();
T Next ();
}
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