Python设计模式之迭代器模式原理与用法实例分析

yipeiwu_com7年前Python基础

本文实例讲述了Python设计模式之迭代器模式原理与用法。分享给大家供大家参考,具体如下:

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

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

#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Andy'
"""
大话设计模式
设计模式——迭代器模式
迭代器模式(Iterator Pattern):提供方法顺序访问一个聚合对象中各元素,而又不暴露该对象的内部表示.
"""
#迭代器抽象类
class Iterator(object):
  def First(self):
    pass
  def Next(self):
    pass
  def Isdone(self):
    pass
  def CurrItem(self):
    pass
#聚集抽象类
class Aggregate(object):
  def CreateIterator(self):
    pass
#具体迭代器类
class ConcreteIterator(Iterator):
  def __init__(self, aggregate):
    self.aggregate = aggregate
    self.curr = 0
  def First(self):
    return self.aggregate[0]
  def Next(self):
    ret = None
    self.curr += 1
    if self.curr < len(self.aggregate):
      ret = self.aggregate[self.curr]
    return ret
  def Isdone(self):
    return True if self.curr+1 >= len(self.aggregate) else False
  def CurrItem(self):
    return self.aggregate[self.curr]
#具体聚集类
class ConcreteAggregate(Aggregate):
  def __init__(self):
    self.ilist = []
  def CreateIterator(self):
    return ConcreteIterator(self)
class ConcreteIteratorDesc(Iterator):
  def __init__(self, aggregate):
    self.aggregate = aggregate
    self.curr = len(aggregate)-1
  def First(self):
    return self.aggregate[-1]
  def Next(self):
    ret = None
    self.curr -= 1
    if self.curr >= 0:
      ret = self.aggregate[self.curr]
    return ret
  def Isdone(self):
    return True if self.curr-1<0 else False
  def CurrItem(self):
    return self.aggregate[self.curr]
if __name__=="__main__":
  ca = ConcreteAggregate()
  ca.ilist.append("大鸟")
  ca.ilist.append("小菜")
  ca.ilist.append("老外")
  ca.ilist.append("小偷")
  itor = ConcreteIterator(ca.ilist)
  print itor.First()
  while not itor.Isdone():
    print itor.Next()
  print "————倒序————"
  itordesc = ConcreteIteratorDesc(ca.ilist)
  print itordesc.First()
  while not itordesc.Isdone():
    print itordesc.Next()

运行结果:

上面类的设计如下图:

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

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

更多关于Python相关内容可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

相关文章

解决pycharm安装后代码区不能编辑的问题

解决pycharm安装后代码区不能编辑的问题

此问题是由于最新的pycharm在安装时自动装了vimVim插件 你可以在tools Vim emulator将对勾去掉就可以了。 以上这篇解决pycharm安装后代码区不能编辑的问题...

python3.4+pycharm 环境安装及使用方法

python3.4+pycharm 环境安装及使用方法

遇到很多初学者的盆友,来问python环境安装的问题。。因此,这篇文章就诞生了。。 因个人是windows的环境,所以本文只讲windows环境下的python安装。 作为初用pytho...

Python使用matplotlib绘制多个图形单独显示的方法示例

Python使用matplotlib绘制多个图形单独显示的方法示例

本文实例讲述了Python使用matplotlib绘制多个图形单独显示的方法。分享给大家供大家参考,具体如下: 一 代码 import numpy as np import matp...

详细介绍Ruby中的正则表达式

详细介绍Ruby中的正则表达式

正则表达式是一种特殊序列的字符,它通过使用有专门语法的模式来匹配或查找其他字符串或字符串集合。 语法 正则表达式从字面上看是一种介于斜杠之间或介于跟在 %r 后的任意分隔符之间的模式,如...

python中matplotlib实现最小二乘法拟合的过程详解

python中matplotlib实现最小二乘法拟合的过程详解

前言 最小二乘法Least Square Method,做为分类回归算法的基础,有着悠久的历史(由马里·勒让德于1806年提出)。它通过最小化误差的平方和寻找数据的最佳函数匹配。利用最小...