1) intent
A method for providing access inside a sequential polymerization of the respective element object, and does not require exposure of the object representation
2) structure
among them:
- Iterator interface definition to access and traverse elements
- ConcreteIterator implement iterator interface, tracking the current location of the traversal time of polymerization
- Aggregate to create the appropriate iterator object defined interface
- ConcreteAggregate implementation creates a corresponding iterator interface
3) Applicability
- Accessing a content object and the polymerization without exposing its internal representation
- Support for multiple traversal of the aggregate object
- Traversing the different polymeric structure to provide a unified interface
4) Examples
. 1 #include <the iostream> 2 typedef int DATE; . 3 class the Iterator . 4 { . 5 public : . 6 the Iterator () {} . 7 Virtual ~ the Iterator () {} . 8 Virtual void First () = 0 ; // iterator zero . 9 Virtual void the next () = 0 ; // iterator to the next 10 Virtual BOOL IsDone () = 0 ; // iterator is pointing to the last element 11 Virtual DATE Currentltem () = 0 ; // iterator is currently pointing element 12 is }; 13 is 14 class Aggregate 15 { 16 public : . 17 Aggregate () {}; 18 is Virtual ~ Aggregate () {} . 19 Virtual the Iterator CreateIterator * () = 0 ; 20 is Virtual int getSize () = 0 ; 21 is Virtual DATE the getItem ( int index) = 0 ; 22 is Virtual void push_back(DATE date) = 0; 23 }; 24 class ConcreteIterator : public Iterator 25 { 26 public: 27 ConcreteIterator(Aggregate* ptr) : m_pAggregate(ptr) 28 ,m_index(0) 29 { 30 } 31 virtual ~ConcreteIterator() {} 32 virtual void First() 33 { 34 m_index = 0; 35 } 36 virtual void Next() 37 { 38 m_index++; 39 } 40 virtual bool IsDone() 41 { 42 if (m_index == m_pAggregate->getSize()) 43 { 44 return true; 45 } 46 return false; 47 } 48 virtual DATE Currentltem() 49 { 50 return m_pAggregate->getItem(m_index); 51 } 52 private: 53 int m_index; 54 Aggregate* m_pAggregate; 55 }; 56 class ConcreteAggregate : public Aggregate 57 { 58 public: 59 ConcreteAggregate(int num) : m_size(num), m_index(0) 60 { 61 m_start = new DATE[num]; 62 } 63 virtual ~ConcreteAggregate() 64 { 65 delete[] m_start; 66 } 67 virtual Iterator* CreateIterator() 68 { 69 return new ConcreteIterator(this); 70 } 71 void push_back(DATE date) 72 { 73 if (m_index < m_size) 74 { 75 *(m_start+m_index) = date; 76 m_index++; 77 } 78 } 79 virtual intgetSize () 80 { 81 return m_index; 82 } 83 Virtual DATE the getItem ( int index) 84 { 85 return * (DATE *) (M_START + index); 86 } 87 Private : 88 DATE * M_START; // use of space in the head current 89 int m_index; 90 int m_size; // apply spatial length 91 is }; 92 93 int main () 94 { 95 Aggregate* pAggregate = new ConcreteAggregate(2); 96 97 pAggregate->push_back(1); 98 pAggregate->push_back(2); 99 Iterator* iter = new ConcreteIterator(pAggregate); 100 for (; !iter->IsDone(); iter->Next()) 101 { 102 std::cout << iter->Currentltem() << std::endl; 103 } 104 delete pAggregate; 105 delete iter; 106 system("pause"); 107 }