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


Posted in Python onJanuary 10, 2019

本文实例讲述了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相关内容可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

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

Python 相关文章推荐
Python中的localtime()方法使用详解
May 22 Python
python中requests小技巧
May 10 Python
matplotlib调整子图间距,调整整体空白的方法
Aug 03 Python
Python内置random模块生成随机数的方法
May 31 Python
在python下使用tensorflow判断是否存在文件夹的实例
Jun 10 Python
pycharm new project变成灰色的解决方法
Jun 27 Python
详解Ubuntu环境下部署Django+uwsgi+nginx总结
Apr 02 Python
xadmin使用formfield_for_dbfield函数过滤下拉表单实例
Apr 07 Python
opencv 图像礼帽和图像黑帽的实现
Jul 07 Python
python环境搭建和pycharm的安装配置及汉化详细教程(零基础小白版)
Aug 19 Python
Python 找出英文单词列表(list)中最长单词链
Dec 14 Python
Python按顺序遍历并读取文件夹中文件
Apr 29 Python
Python设计模式之桥接模式原理与用法实例分析
Jan 10 #Python
Python基础教程之异常详解
Jan 10 #Python
Python+OpenCV感兴趣区域ROI提取方法
Jan 10 #Python
python+opencv 读取文件夹下的所有图像并批量保存ROI的方法
Jan 10 #Python
pandas ix &amp;iloc &amp;loc的区别
Jan 10 #Python
python 移动图片到另外一个文件夹的实例
Jan 10 #Python
python将处理好的图像保存到指定目录下的方法
Jan 10 #Python
You might like
PHP简单系统数据添加以及数据删除模块源文件下载
2008/06/07 PHP
利用PHP函数计算中英文字符串长度的方法
2014/11/11 PHP
PHP依赖注入(DI)和控制反转(IoC)详解
2017/06/12 PHP
PHP面向对象程序设计之接口的继承定义与用法详解
2018/12/20 PHP
Thinkphp5.0 框架使用模型Model添加、更新、删除数据操作详解
2019/10/11 PHP
js版本A*寻路算法
2006/12/22 Javascript
Firefox getBoxObjectFor getBoundingClientRect联系
2008/10/26 Javascript
javascript编码的几个方法详细介绍
2013/01/06 Javascript
node.js中的http.response.removeHeader方法使用说明
2014/12/14 Javascript
javascript学习小结之prototype
2015/12/03 Javascript
javascript设计模式之Adapter模式【适配器模式】实现方法示例
2017/01/13 Javascript
深入理解Angular.JS中的Scope继承
2017/06/04 Javascript
jquery中attr、prop、data区别与用法分析
2019/09/25 jQuery
js找出5个数中最大的一个数和倒数第二大的数实现方法示例小结
2020/03/04 Javascript
Python统计列表中的重复项出现的次数的方法
2014/08/18 Python
线程和进程的区别及Python代码实例
2015/02/04 Python
整理Python 常用string函数(收藏)
2016/05/30 Python
asyncio 的 coroutine对象 与 Future对象使用指南
2016/09/11 Python
Linux 下 Python 实现按任意键退出的实现方法
2016/09/25 Python
python实现学生管理系统
2018/01/11 Python
Python实现替换文件中指定内容的方法
2018/03/19 Python
Python装饰器原理与用法分析
2018/04/30 Python
python3让print输出不换行的方法
2020/08/24 Python
python pillow库的基础使用教程
2021/01/13 Python
HTML5边玩边学(2)基础绘图实现方法
2010/09/21 HTML / CSS
商务英语专业自荐信
2013/10/14 职场文书
高中毕业自我鉴定
2013/12/22 职场文书
股东合作协议书
2014/04/14 职场文书
总经理检讨书
2014/09/15 职场文书
小学生教师节广播稿
2015/08/19 职场文书
pytorch中[..., 0]的用法说明
2021/05/20 Python
如何用PHP实现分布算法之一致性哈希算法
2021/05/26 PHP
Python还能这么玩之用Python做个小游戏的外挂
2021/06/04 Python
python图像处理基本操作总结(PIL库、Matplotlib及Numpy)
2021/06/08 Python
图片批量处理 - 尺寸、格式、水印等
2022/03/07 杂记
Sentry的安装、配置、使用教程(Sentry日志手机系统)
2022/07/23 Python