Iterator Design Pattern using Python
Iterator Design Pattern

Iterator Design Pattern using Python

Iterator design pattern gives a way to access elements of a Collection in a sequential manner. It does not have to know the underlying working for the iteration to work.

Key Components:

  1. Client - Class or function that contains a collection object and calls the next() method to access the collection items
  2. Iterator Interface - Interface that defines the operations to ACCESS the collection items
  3. Concrete Interface - Concrete class that implements the operations to ACCESS the collection items
  4. Collection / Aggregator Interface - Interface that defines the operations to CREATE the collection
  5. Concrete Collection / Aggregator - Concrete class that implements the operations to CREATE the collection

from abc import ABC

class IShoppingListIterator(ABC):
    def has_next(self):
        pass

    def next(self):
        pass


class IShoppingListCollection(ABC):
    def createIterator(self) -> IShoppingListIterator:
        pass


class ShoppingListIterator(IShoppingListIterator):
    def __init__(self, shopping_list: list[str]) -> None:
        self._shopping_list = shopping_list
        self._current_index = 0

    def has_next(self):
        return self._current_index < len(self._shopping_list)
            
    def next(self):
        current_item = self._shopping_list[self._current_index]
        self._current_index += 1
        return current_item
        

class ShoppingListCollection(IShoppingListCollection):
    def __init__(self, shopping_list) -> None:
        self._shopping_list = shopping_list

    def createIterator(self) -> IShoppingListIterator:
        return ShoppingListIterator(self._shopping_list)
    

def main():
    shopping_list = ["eggs", "apples", "butter"]
    aggregator = IShoppingListCollection(shopping_list=shopping_list)
    iterator = aggregator.createIterator()

    while iterator.has_next():
        print(iterator.next())

main()        

Please find the notebook on Google Colab for a simple implementation of Iterator Design Pattern using #Python.?More implementations will be posted soon!

https://colab.research.google.com/github/penningjoy/designpatternswithpython/blob/main/Iterator_Design_Pattern.ipynb

Happy learning!

#softwaredevelopment #softwaredesignpatterns

要查看或添加评论,请登录

Joydeep Banerjee的更多文章

  • Adapter Design Pattern

    Adapter Design Pattern

    The Adapter Design Pattern is a structural design pattern that allows objects with incompatible interfaces to work…

  • Understanding Product Market fit for Product Development

    Understanding Product Market fit for Product Development

    Are you are running a business that involves building products and you often wonder if your products will ever be…

  • Musings of the May

    Musings of the May

    These are indeed harrowing times!! On the back of the lethal outbreak of the pandemic, Calcutta, my hometown, recently…

  • Model Selection approaches in Automated Machine Learning- a quick comparison

    Model Selection approaches in Automated Machine Learning- a quick comparison

    Model Selection is one of the most critical pieces in building an Automated Machine Learning Platform. These platforms…

  • Data Analysis in Supply Chain Management

    Data Analysis in Supply Chain Management

    Data Analysis is an integral part of running a business of today of any size across all industries. It is crucial to…

  • Smart Grid Analytics- your next smart choice

    Smart Grid Analytics- your next smart choice

    Global Warming is the curse of 21st century and it's already showing its impact on climate change. It's imperative that…

  • Accept Yourself and Conquer

    Accept Yourself and Conquer

    Intelligence, in its general sense, is highly over-rated and terribly flawed. It is as hypocritical as the perception…

    2 条评论
  • A happy mind is a rich mind

    A happy mind is a rich mind

    Happiness is not just the most cherished state of mind but also a secret to productivity. A happy mind can achieve much…

  • Start or be left out

    Start or be left out

    Ever wondered why exactly only a few get to have the lion's share of success while millions stay lost in the crowd…

    3 条评论

社区洞察

其他会员也浏览了