Python設(shè)計(jì)模式之迭代器模式原理與用法實(shí)例分析
本文實(shí)例講述了Python設(shè)計(jì)模式之迭代器模式原理與用法。分享給大家供大家參考,具體如下:
迭代器模式(Iterator Pattern):提供方法順序訪問一個(gè)聚合對象中各元素,而又不暴露該對象的內(nèi)部表示.
下面是一個(gè)迭代器模式的demo:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Andy'
"""
大話設(shè)計(jì)模式
設(shè)計(jì)模式——迭代器模式
迭代器模式(Iterator Pattern):提供方法順序訪問一個(gè)聚合對象中各元素,而又不暴露該對象的內(nèi)部表示.
"""
#迭代器抽象類
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()
運(yùn)行結(jié)果:

上面類的設(shè)計(jì)如下圖:

當(dāng)需要對聚集有多種方式遍歷時(shí),可以考慮使用迭代器模式
迭代器模式分離了集合的遍歷行為,抽象出一個(gè)迭代器類來負(fù)責(zé),這樣既可以做到不暴露集合內(nèi)部結(jié)構(gòu),又可以讓外部代碼透明的訪問集合內(nèi)部的數(shù)據(jù)
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python pandas消除空值和空格以及 Nan數(shù)據(jù)替換方法
今天小編就為大家分享一篇python pandas消除空值和空格以及 Nan數(shù)據(jù)替換方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python學(xué)習(xí)之a(chǎn)syncore模塊用法實(shí)例教程
這篇文章主要介紹了Python學(xué)習(xí)之a(chǎn)syncore模塊用法,主要講述了asyncore模塊的組成、原理及相關(guān)函數(shù)的用法,對于使用Python進(jìn)行網(wǎng)絡(luò)編程來說非常實(shí)用,需要的朋友可以參考下2014-09-09
Python GUI編程學(xué)習(xí)筆記之tkinter界面布局顯示詳解
這篇文章主要介紹了Python GUI編程學(xué)習(xí)筆記之tkinter界面布局顯示,結(jié)合實(shí)例形式分析了Python GUI編程中tkinter界面布局顯示的相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2020-03-03
Python使用sklearn實(shí)現(xiàn)的各種回歸算法示例
這篇文章主要介紹了Python使用sklearn實(shí)現(xiàn)的各種回歸算法,結(jié)合實(shí)例形式分析了Python使用sklearn庫實(shí)現(xiàn)的決策樹回歸、線性回歸、SVM回歸、KNN回歸、隨機(jī)森林回歸等各種回歸算法,需要的朋友可以參考下2019-07-07
深入理解Python中的*args和**kwargs參數(shù)(示例代碼)
*args和**kwargs是Python函數(shù)編程中極其有用的特性,它們?yōu)楹瘮?shù)參數(shù)的處理提供了極大的靈活性和強(qiáng)大的功能,這篇文章主要介紹了Python中的*args和**kwargs參數(shù),需要的朋友可以參考下2024-06-06
Python基于pywinauto實(shí)現(xiàn)的自動(dòng)化采集任務(wù)
這篇文章主要介紹了Python基于pywinauto實(shí)現(xiàn)的自動(dòng)化采集任務(wù),模擬了輸入單詞, 復(fù)制例句, 獲取例句, 清空剪切板, 然后重復(fù)這個(gè)操作,需要的朋友可以參考下2023-04-04

