-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathiterator_concept.py
61 lines (45 loc) · 1.49 KB
/
iterator_concept.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
# pylint: disable=too-few-public-methods
# pylint: disable=arguments-differ
"The Iterator Pattern Concept"
from abc import ABCMeta, abstractmethod
class IIterator(metaclass=ABCMeta):
"An Iterator Interface"
@staticmethod
@abstractmethod
def has_next():
"Returns Boolean whether at end of collection or not"
@staticmethod
@abstractmethod
def next():
"Return the object in collection"
class Iterable(IIterator):
"The concrete iterator (iterable)"
def __init__(self, aggregates):
self.index = 0
self.aggregates = aggregates
def next(self):
if self.index < len(self.aggregates):
aggregate = self.aggregates[self.index]
self.index += 1
return aggregate
raise Exception("AtEndOfIteratorException", "At End of Iterator")
def has_next(self):
return self.index < len(self.aggregates)
class IAggregate(metaclass=ABCMeta):
"An interface that the aggregates should implement"
@staticmethod
@abstractmethod
def method():
"a method to implement"
class Aggregate(IAggregate):
"A concrete object"
@staticmethod
def method():
print("This method has been invoked")
# The Client
AGGREGATES = [Aggregate(), Aggregate(), Aggregate(), Aggregate()]
# AGGREGATES is a python list that is already iterable by default.
# but we can create own own iterator on top anyway.
ITERABLE = Iterable(AGGREGATES)
while ITERABLE.has_next():
ITERABLE.next().method()