• [python实现设计模式]-5.迭代器模式-一起撸串嗨皮啦


    迭代器模式是一个我们经常使用但是出境不高的模式。

    为啥捏?因为大部分的语言都帮我们实现了细节,我们不许关注他的实现就能用的很嗨皮了。

    不管怎样。这也是个非常常用的模式.

    俗话说得好,这个世界上没有事情是一顿撸串解决不了的,如果有,那就是两顿撸串。

    那么,我们今天的故事就从撸串说起。

    众人在撸串中.大师兄一拍桌子.来来来...大家一起走一个...干!!!了....

    众人都干了...

    一分钟后...大师兄一拍桌子.来来来...大家一起走一个...干!!!了....

    众人又干了.....

    一分钟后...大师兄一拍桌子.来来来...大家一起走一个...干!!!了....

    众人纷纷尿了.....表示,太高能了...搞不下去了.....

    又过了一分钟...大师兄说,你们真怂.....来来来,我挨个单挑你们这群战斗力为0的渣渣......

    于是...大师兄按照顺时针方向挨个单挑....

    first , 旭明,  next 帅哥,  next 志xin, next.....

    谁也跑不了...

    那么,这就是个迭代器模式.

    ---------------------------------------

    概述:                                                                                                      

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

    实用场合:                                                                                                 

    1.访问一个聚合对象的内容而无需暴露它的内部表示。

    2.支持对聚合对象的多种遍历。

    3.为遍历不同的聚合结构提供一个统一的接口(即,多态迭代)。

    ---------------------------------------

    以上,是我抄得..

    按照惯例,上例图.

    so, 我们用python来实现这个过程.

    # -*- coding: utf-8 -*-
    
    from abc import ABCMeta, abstractmethod
    
    
    #迭代器抽象类
    class Iterator(object):
    
        __metaclass__ = ABCMeta
    
        def __init__(self):
            pass
    
        @abstractmethod
        def Frist(self):
            pass
    
        @abstractmethod
        def Next(self):
            pass
    
        @abstractmethod
        def Isdone(self):
            pass
    
        @abstractmethod
        def CurrentItem(self):
            pass
    
    #聚集抽象类
    class Aggregate(object):
        __metaclass__ = ABCMeta
    
        def __init__(self):
            pass
    
        @abstractmethod
        def CreateIterator(self):
            pass
    
    #迭代器具体实现类
    class ConcreteIterator(Iterator):
    
        def __init__(self, concreteAggregate):
            self.__concreteAggregate = concreteAggregate
            self.__current = 0
    
        def Isdone(self):
            return True if self.__current >= self.__concreteAggregate.Count else False
    
        def Next(self):
            self.__current += 1
            if self.__current < self.__concreteAggregate.Count:
                return self.__concreteAggregate.GetItem(self.__current)
    
        def CurrentItem(self):
            return self.__concreteAggregate.GetItem(self.__current)
    
        def Frist(self):
            return self.__concreteAggregate.GetItem(0)
    
    #实现聚集类
    class ConcreteAggregate(Aggregate):
    
        Items = None
    
        #实现抽象方法
        def CreateIterator(self):
            return ConcreteAggregate()
    
        def __init__(self):
            ConcreteAggregate.Items = []
    
        @property
        def Count(self):
            return ConcreteAggregate.Items.__len__()
    
        def GetItem(self, index):
            return ConcreteAggregate.Items[index]
    
    
    if __name__ == "__main__":
    
        concreteAggregate = ConcreteAggregate()
        concreteAggregate.Items.append('xuming')
        concreteAggregate.Items.append('帅哥')
        concreteAggregate.Items.append('zhixin')
        concreteAggregate.Items.append('高峰')
        concreteAggregate.Items.append('创始人')
        concreteAggregate.Items.append('小灰灰')
    
        print "大师兄开始单挑了!!!....
    "
    
        iterator = ConcreteIterator(concreteAggregate)
    
        obj = iterator.Frist()
        while not iterator.Isdone():
            print "
    撸撸撸...  该你啦!", iterator.CurrentItem()
            print "这里假装在喝酒"
            print iterator.CurrentItem(), "假装喝完了"
            print "大师兄休息几秒钟...
    ", "*" * 10
            iterator.Next()


    运行结果:

    最后再说两句:

    这是一个非常常用的模式,但是它太常用了,所以很多语言都内置了该模式,

    这反而让我们觉得这是个冷门的模式了.

    比如.net framework 已经准备好了迭代器接口,只需要实现接口就行了

    IEumerator 支持对非泛型集合的简单迭代

    上面的例子用一个foreach就可以实现

    其实吧, foreach 关键字就是一个语法糖, 背后实现的原理就是迭代器模式.

    另外我们可以使用yield 关键字来简化迭代.

    详见:

    https://msdn.microsoft.com/zh-cn/library/65zzykke(v=vs.100).aspx

    以上,就是迭代器模式。

    希望对你有所帮助.

    to be continued.

  • 相关阅读:
    尚硅谷SpringBoot
    try-with-resources
    Spring中的InitializingBean接口
    @Deprecated注解
    OpenStack 九大组建介绍
    rainbow 实现云上迁移
    推送一个私有镜像到harbor
    搭建企业级私有registry Harbor
    Linux 部署 jenkins
    OpenStack 发放虚拟机流程
  • 原文地址:https://www.cnblogs.com/kakaliush/p/5327293.html
Copyright © 2020-2023  润新知