-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.cpp
88 lines (81 loc) · 1.81 KB
/
1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
#include<vector>
using namespace std;
template<typename T>
class Iterator
{
public:
virtual void First() = 0;
virtual void Next() = 0;
virtual bool IsDone() = 0;
virtual T CurrentItem() const = 0;
};
template<typename T>
class Aggregate
{
public:
virtual Iterator<T>* CreateIterator() const = 0;
virtual long Count() const = 0;
virtual T Get(long index) const = 0;
};
template<typename T>
class ConcreteAggregate: public Aggregate<T>
{
private:
vector<T> data;
public:
ConcreteAggregate(vector<T> myData):data(myData) {}
virtual Iterator<T>* CreateIterator() const;
virtual long Count() const
{
return data.size();
}
virtual T Get(long index) const
{
return data[index];
}
};
template<typename T>
class ConcreteIterator: public Iterator<T>
{
private:
const Aggregate<T>* aggregate;
long current;
public:
ConcreteIterator(const Aggregate<T>* myAggregate): //const必须有
aggregate(myAggregate),current(0) {}
virtual void First()
{
current = 0;
}
virtual void Next()
{
current ++;
}
virtual bool IsDone()
{
return current >= aggregate -> Count();
}
virtual T CurrentItem() const
{
return aggregate -> Get(current);
}
};
template<typename T>
Iterator<T>* ConcreteAggregate<T>::CreateIterator() const
{
Iterator<T>* iterator = new ConcreteIterator<T>(this);
return iterator;
}
int main()
{
vector<int> myData = {1,2,3,4,5};
Aggregate<int>* myAggregate = new ConcreteAggregate<int>(myData);
Iterator<int>* myIterator = myAggregate -> CreateIterator();
// new ConcreteIterator<int>(myAggregate);
for (myIterator -> First(); !myIterator -> IsDone(); myIterator -> Next())
{
cout << "the current value is " << myIterator -> CurrentItem() << endl;
}
return 0;
}