As a material for learning GoF design patterns, the book "Introduction to Design Patterns Learned in the Augmented and Revised Java Language" seems to be helpful. However, since the examples taken up are based on JAVA, I tried the same practice in Python to deepen my understanding.
The Iterator pattern is one of the design patterns defined by GoF (Gang of Four; 4 gangs). The purpose is to provide an iterator that does not depend on the internal specifications of the container by making the means for enumerating the elements of the container object independent.
UML class and sequence diagram UML class diagram (The above is quoted from Wikipedia)
Iterator pattern seems to be for performing the process of pointing to it in order and scanning the whole when a lot of things are gathered. ʻIterator` is sometimes called ** iterator ** in Japanese. You often see it when you're involved in Python programming.
I would like to actually run a sample program that utilizes the Iterator pattern and check the following behavior.
--Add ʻAroun d the World in 80 daysbooks to the bookshelf --Add
Biblebooks to the bookshelf --Add
Cinderellabooks to the bookshelf --Add the
Daddy-Long-Legs` book to the bookshelf
--Display the titles of books currently on the bookshelf
$ python Main.py
Aroun d the World in 80 days
Bible
Cinderella
Daddy-Long-Legs
Similar code has been uploaded to the Git repository. https://github.com/ttsubo/study_of_design_pattern/tree/master/Iterator/step1
--Directory structure
.
├── Main.py
└── iterator
├── __init__.py
├── aggregate.py
├── book.py
└── iterator.py
It is the role that defines the interface that scans the elements in order. In the sample program, the ʻIterator` class serves this role.
iterator/iterator.py
from abc import ABCMeta, abstractmethod
class Iterator(metaclass=ABCMeta):
@abstractmethod
def hasNext(self):
pass
@abstractmethod
def next(self):
pass
It is the role that actually implements the interface defined by the role of ʻIterator. In the sample program, the
BookShelfIteratorclass serves this role. This role must have the information needed to scan. In the sample program, the instance of the
BookShelf class is remembered by the instance variable
self.__ bookShelf, and the book of interest is remembered by the instance variable
self.__index`.
iterator/book.py
from iterator.iterator import Iterator
...(snip)
class BookShelfIterator(Iterator):
def __init__(self, bookShelf):
self.__bookShelf = bookShelf
self.__index = 0
def hasNext(self):
return True if self.__index < self.__bookShelf.getLength() else False
def next(self):
book = self.__bookShelf.getBookAt(self.__index)
self.__index += 1
return book
ʻIterator This is the role that defines the interface that creates the role. The interface is an abstraction method that creates "a person who scans the elements I have in order". In the sample program, the ʻAggregate
class serves this role.
iterator/aggregate.py
from abc import ABCMeta, abstractmethod
class Aggregate(metaclass=ABCMeta):
@abstractmethod
def iterator(self):
pass
ʻAggregate This is the role that actually implements the interface defined by the role. Create a concrete instance of the ʻIterator
role, that is, the Concrete Iterator
role.
In the sample program, the BookShelf
class serves this role.
iterator/book.py
from iterator.aggregate import Aggregate
...(snip)
class BookShelf(Aggregate):
def __init__(self, maxSize):
self.__last = 0
self.__books = [None] * maxSize
def getBookAt(self, index):
return self.__books[index]
def append(self, book):
self.__books[self.__last] = book
self.__last += 1
def getLength(self):
return self.__last
def iterator(self):
return BookShelfIterator(self)
In the sample program, the startMain
method serves this role.
Main.py
from iterator.book import Book, BookShelf
def startMain():
bookShelf = BookShelf(4)
bookShelf.append(Book(name="Aroun d the World in 80 days"))
bookShelf.append(Book(name="Bible"))
bookShelf.append(Book(name="Cinderella"))
bookShelf.append(Book(name="Daddy-Long-Legs"))
it = bookShelf.iterator()
while it.hasNext():
book = it.next()
print(book.getName())
if __name__ == '__main__':
startMain()
Manage book titles.
iterator/book.py
class Book(object):
def __init__(self, name):
self.__name = name
def getName(self):
return self.__name
In Python programming, I often see iterators. For information on Python iterators, refer to this Web article "[Introduction to Python] What is an Iterator?". .. In addition, when defining a new class, it seems that the following requirements must be met in order to have the properties of an iterator.
--Has a __iter__
method that returns itself
--Has a __next__
method that returns the values of the element columns that you manage one by one
--In the __next__
method, aStopIteration
exception is raised when the elements are exhausted.
I would like to rewrite the sample program with ** Python Iterator **. Similar code has been uploaded to the Git repository. https://github.com/ttsubo/study_of_design_pattern/tree/master/Iterator/step2
iterator/book.py
class Book(object):
def __init__(self, name):
self.__name = name
def getName(self):
return self.__name
class BookShelf(object):
def __init__(self):
self.__books = []
def append(self, book):
self.__books.append(book)
def __iter__(self):
self.__index = 0
return self
def __next__(self):
if self.__index >= len(self.__books):
raise StopIteration()
book = self.__books[self.__index]
self.__index += 1
return book
Main.py
from iterator.book import Book, BookShelf
def startMain():
bookShelf = BookShelf()
bookShelf.append(Book(name="Aroun d the World in 80 days"))
bookShelf.append(Book(name="Bible"))
bookShelf.append(Book(name="Cinderella"))
bookShelf.append(Book(name="Daddy-Long-Legs"))
for book in bookShelf:
print(book.getName())
if __name__ == '__main__':
startMain()
It's a lot simpler. Let's move it.
$ python Main.py
Aroun d the World in 80 days
Bible
Cinderella
Daddy-Long-Legs
Since it is the same as the first operation result, it is completed for the time being.
-[Finishing "Introduction to Design Patterns Learned in Java Language" (Not)](https://medium.com/since-i-want-to-start-blog-that-looks-like-men-do/java Introduction to Design Patterns Learned in Language-Finishing-Not-2cc9b34a30b2) -Web article "[Introduction to Python] What is an iterator?"
Recommended Posts