-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathIteratorPattern.py
100 lines (69 loc) · 1.79 KB
/
IteratorPattern.py
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
89
90
91
92
93
94
95
96
97
98
99
100
# coding: utf8
from abc import ABCMeta, abstractmethod
class Iterator(object):
__metaclass__ = ABCMeta
@abstractmethod
def has_next(self):
pass
@abstractmethod
def next(self):
pass
@abstractmethod
def remove(self):
pass
@abstractmethod
def rewind(self):
pass
class ListIterator(Iterator):
def __init__(self, my_list):
self._my_list = my_list
self._cursor = 0
def has_next(self):
return self._cursor < self._my_list.size()
def next(self):
self._cursor = self._cursor + 1
return self._my_list.get(self._cursor - 1)
def remove(self):
self._cursor = self._cursor - 1
return self._my_list.remove(self._cursor)
def rewind(self):
self._cursor = 0
class MyList(object):
__metaclass__ = ABCMeta
@abstractmethod
def add(self, element):
pass
@abstractmethod
def get(self, index):
pass
@abstractmethod
def remove(self, index):
pass
@abstractmethod
def size(self):
pass
@abstractmethod
def iterator(self):
pass
class MyListImpl(MyList):
def __init__(self):
self._inner_list = []
def add(self, element):
self._inner_list.append(element)
def get(self, index):
return self._inner_list[index]
def remove(self, index):
return self._inner_list.pop(index)
def size(self):
return len(self._inner_list)
def iterator(self):
return ListIterator(self)
if __name__ == "__main__":
my_list = MyListImpl()
for element in range(10):
my_list.add(element)
iterator = my_list.iterator()
while iterator.has_next():
print(iterator.next())
iterator.remove()
assert my_list.size() == 0