激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python設計模式之迭代器模式原理與用法實例分析

Python設計模式之迭代器模式原理與用法實例分析

2021-05-14 00:09Andy Python

這篇文章主要介紹了Python設計模式之迭代器模式原理與用法,結合具體實例形式分析了迭代器模式的概念、原理、定義及使用方法,代碼注釋說明簡單易懂,需要的朋友可以參考下

本文實例講述了python設計模式迭代器模式原理與用法。分享給大家供大家參考,具體如下:

迭代器模式(iterator pattern):提供方法順序訪問一個聚合對象中各元素,而又不暴露該對象的內部表示.

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/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程序設計有所幫助。

原文鏈接:https://www.cnblogs.com/onepiece-andy/p/python-iterator-pattern.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 国产黄色一级大片 | 暖暖免费观看高清完整版电影 | av免费在线观看国产 | 欧美一级黄色免费看 | 久久久久九九九女人毛片 | 欧美福利视频一区二区三区 | 久久黄色影院 | 久久色播| h视频免费在线观看 | 国产亚洲高清在线精品不卡 | 免费激情网站 | 欧美日韩免费在线观看视频 | 欧美日韩a∨毛片一区 | 日日狠狠久久偷偷四色综合免费 | 凹凸成人精品亚洲精品密奴 | 日本aaaa片毛片免费观蜜桃 | 国产免费观看视频 | av在线直播观看 | 91精品国产91久久久久久丝袜 | 操操操操网 | 青青草好吊色 | www.国产.com| 日本免费靠逼视频 | 中文字幕一二三区芒果 | 99久久自偷自偷国产精品不卡 | 全黄毛片| 久久久久久久黄色片 | 国产孕妇孕交大片孕 | 免费国产 | 国产一区二区三区高清 | 午夜精品在线视频 | 精品一区二区三区网站 | 97干色 | 亚洲无马在线观看 | 精品国产高清一区二区三区 | 婷婷精品国产一区二区三区日韩 | 亚洲成人福利电影 | 91九色视频观看 | 欧美韩国一区 | 亚洲成人中文字幕在线 | 国产精品三级a三级三级午夜 |