• 大话设计模式Python实现-迭代器模式


    迭代器模式(Iterator Pattern):提供方法顺序访问一个聚合对象中各元素,而又不暴露该对象的内部表示.

     下面是一个迭代器模式的demo:

     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 
     4 __author__ = 'Andy'
     5 """
     6 大话设计模式
     7 设计模式——迭代器模式
     8 迭代器模式(Iterator Pattern):提供方法顺序访问一个聚合对象中各元素,而又不暴露该对象的内部表示.
     9 """
    10 #迭代器抽象类
    11 class Iterator(object):
    12     def First(self):
    13         pass
    14 
    15     def Next(self):
    16         pass
    17 
    18     def Isdone(self):
    19         pass
    20 
    21     def CurrItem(self):
    22         pass
    23 
    24 #聚集抽象类
    25 class Aggregate(object):
    26 
    27     def CreateIterator(self):
    28         pass
    29 
    30 #具体迭代器类
    31 class ConcreteIterator(Iterator):
    32 
    33     def __init__(self, aggregate):
    34         self.aggregate = aggregate
    35         self.curr = 0
    36 
    37     def First(self):
    38         return self.aggregate[0]
    39 
    40     def Next(self):
    41         ret = None
    42         self.curr += 1
    43         if self.curr < len(self.aggregate):
    44             ret = self.aggregate[self.curr]
    45         return ret
    46 
    47     def Isdone(self):
    48         return True if self.curr+1 >= len(self.aggregate) else False
    49 
    50     def CurrItem(self):
    51         return self.aggregate[self.curr]
    52 
    53 #具体聚集类
    54 class ConcreteAggregate(Aggregate):
    55 
    56     def __init__(self):
    57         self.ilist = []
    58 
    59     def CreateIterator(self):
    60         return ConcreteIterator(self)
    61 
    62 class ConcreteIteratorDesc(Iterator):
    63     def __init__(self, aggregate):
    64         self.aggregate = aggregate
    65         self.curr = len(aggregate)-1
    66 
    67     def First(self):
    68         return self.aggregate[-1]
    69 
    70     def Next(self):
    71         ret = None
    72         self.curr -= 1
    73         if self.curr >= 0:
    74             ret = self.aggregate[self.curr]
    75         return ret
    76 
    77     def Isdone(self):
    78         return True if self.curr-1<0 else False
    79 
    80     def CurrItem(self):
    81         return self.aggregate[self.curr]
    82 
    83 if __name__=="__main__":
    84     ca = ConcreteAggregate()
    85     ca.ilist.append("大鸟")
    86     ca.ilist.append("小菜")
    87     ca.ilist.append("老外")
    88     ca.ilist.append("小偷")
    89 
    90     itor = ConcreteIterator(ca.ilist)
    91     print itor.First()
    92     while not itor.Isdone():
    93         print itor.Next()
    94     print "————倒序————"
    95     itordesc = ConcreteIteratorDesc(ca.ilist)
    96     print itordesc.First()
    97     while not itordesc.Isdone():
    98         print itordesc.Next()

     上面类的设计如下图:

    当需要对聚集有多种方式遍历时,可以考虑使用迭代器模式

    迭代器模式分离了集合的遍历行为,抽象出一个迭代器类来负责,这样既可以做到不暴露集合内部结构,又可以让外部代码透明的访问集合内部的数据


    作者:Andy
    出处:http://www.cnblogs.com/onepiece-andy/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    7.12.2
    7.12.1
    7.11.8
    循环测试条件前缀和后缀的区别
    7.11.7 两个版本
    7.11.5
    7.12 vowels.c 程序
    7.11 animals.c 程序
    7.6.2 break 语句
    7.10 break.c 程序
  • 原文地址:https://www.cnblogs.com/onepiece-andy/p/python-iterator-pattern.html
Copyright © 2020-2023  润新知