※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

Iteratorパターンの例です。
気が向けばそのうち解説文を書きます。


一般的なIteratorパターンの実装例

Java

  1. package sample.design_pattern;
  2.  
  3. interface Iterator {
  4. public abstract boolean hasNext();
  5. public abstract void next();
  6.  
  7. public abstract Object getValue();
  8. }
  9.  
  10.  
  11.  
  12. interface Aggregate {
  13. public abstract Iterator iterator();
  14. }
  15.  
  16.  
  17.  
  18. class Element {
  19. private int value;
  20.  
  21. public Element(int value) {
  22. this.value = value;
  23. }
  24.  
  25. public int getValue() {
  26. return value;
  27. }
  28.  
  29. public void setValue(int value) {
  30. this.value = value;
  31. }
  32. }
  33.  
  34.  
  35.  
  36. class ElementIterator implements Iterator {
  37. private ElementList list;
  38. private int index;
  39.  
  40. public ElementIterator(ElementList list) {
  41. this.list = list;
  42. this.index = 0;
  43. }
  44.  
  45. public boolean hasNext() {
  46. return (list.getLastNum() > index) ? true : false;
  47. }
  48.  
  49. public void next() {
  50. ++index;
  51. }
  52.  
  53. public Object getValue() {
  54. return list.getValueAt(index);
  55. }
  56. }
  57.  
  58.  
  59.  
  60. class ElementList implements Aggregate {
  61. private Element[] list;
  62. private int last = 0;
  63.  
  64. public ElementList(int count) {
  65. this.list = new Element[count];
  66. }
  67.  
  68. public void add(Element element) {
  69. list[last++] = element;
  70. }
  71.  
  72. public Element getValueAt(int index) {
  73. return list[index];
  74. }
  75.  
  76. public int getLastNum() {
  77. return last;
  78. }
  79.  
  80. public Iterator iterator() {
  81. return new ElementIterator(this);
  82. }
  83. }
  84.  
  85.  
  86.  
  87. public class IteratorPatternTest {
  88. public static void main(String[] args) {
  89. ElementList list = new ElementList(5);
  90.  
  91. for (int i = 0; i < 5; ++i) {
  92. list.add(new Element(i));
  93. }
  94.  
  95. for (Iterator it = list.iterator(); it.hasNext(); it.next()) {
  96. System.out.println(((Element)it.getValue()).getValue());
  97. }
  98. }
  99. }
  100.  

C++

※ 暫定版
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <typename T>
  5. struct iteratable {
  6. virtual ~iteratable() {}
  7.  
  8. virtual const bool has_next() = 0;
  9. virtual void next() = 0;
  10. virtual const T get_value() = 0;
  11. };
  12.  
  13. template <typename Iterator>
  14. struct aggregatable {
  15. virtual ~aggregatable() {}
  16. virtual Iterator iterator_begin() = 0;
  17. };
  18.  
  19. template <typename T, typename List>
  20. class element_iterator : public iteratable<T> {
  21. const List &list_;
  22. size_t index_;
  23.  
  24. public :
  25. element_iterator(const List &list) : list_(list), index_(0) {}
  26.  
  27. const bool has_next() {
  28. return (list_.get_last_num() > index_) ? true : false;
  29. }
  30.  
  31. void next() { ++index_; }
  32.  
  33. const T get_value() { return list_.get_value_at(index_); }
  34. };
  35.  
  36. template <typename T>
  37. class element_list : public aggregatable<element_iterator<T, element_list<T> > > {
  38. T *plist_;
  39. size_t last_;
  40.  
  41. public :
  42. typedef element_iterator<T, element_list<T> > iterator;
  43.  
  44. element_list(const size_t count) : plist_(new T[count]), last_(0) {
  45. }
  46.  
  47. ~element_list() {
  48. delete [] plist_;
  49. }
  50.  
  51. void add(const T val) {
  52. plist_[last_++] = val;
  53. }
  54.  
  55. const T get_value_at(const size_t index) const {
  56. return plist_[index];
  57. }
  58.  
  59. const size_t get_last_num() const {
  60. return last_;
  61. }
  62.  
  63. element_iterator<T, element_list<T>> iterator_begin() {
  64. return element_iterator<T, element_list<T> >(*this);
  65. }
  66. };
  67.  
  68. int main() {
  69. element_list<int> list(5);
  70.  
  71. for (int i = 0; i < 5; ++i) {
  72. list.add(i);
  73. }
  74.  
  75. element_list<int>::iterator it = list.iterator_begin();
  76. for ( it; it.has_next(); it.next()) {
  77. cout << it.get_value() << endl;
  78. }
  79. }
  80.