首页 > 其他分享 >设计模式利剑14-迭代器模式

设计模式利剑14-迭代器模式

时间:2022-11-09 17:02:47浏览次数:38  
标签:End 14 迭代 GetEnumerator ByVal list 设计模式 name


定      义:它提供一种方法访问一个容器对象中各个元素,而又不需暴露该对对象的内部细节,,Iterator模式就是分离了集合对象的遍历行为,抽象出一个迭代器类来负责,这样既可以做到不暴露集合的内部结构,又可让外部代码透明的访问集合内部的数据。

效果及实现要点

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

2.迭代多态:为遍历不同的集合结构提供一个统一的接口,从而支持同样的算法在不同的集合结构上进行操作。

3.迭代器的健壮性考虑:遍历的同时更改迭代器所在的集合结构,会导致问题。

适用性

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

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

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

应用举例:

          先来看看迭代器模式的通用类图:

设计模式利剑14-迭代器模式_function

        解释说明:

        1、Iterator抽象迭代器:抽象迭代器负责定义访问和遍历元素的接口,而且基本上是有固定的方法,first,next,isDone

        2、Aggregate抽象容器:负责提供创建具体迭代器角色的几口,必须提供一个类似createIterator()这样的方法

       举个简单的例子,家中的电视机遥控器控制电视,上一个频道,下一个频道,UML图如下:

设计模式利剑14-迭代器模式_设计模式_02

Public Class ProjectEnumer    Implements System.Collections.IEnumerable
Public Sub New(ByVal __list As Collection)
_list = __list
End Sub
Private _list As Collection
Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return _list.GetEnumerator
End Function
End Class ' ProjectEnumer
Public Interface IProduct

'''
''' <param name="cost"></param>
''' <param name="num"></param>
''' <param name="name"></param>
Sub add(ByVal cost As Integer, ByVal num As Integer, ByVal name As String)
Function getProjectInfo() As String
Function GetEnumerator() As System.Collections.IEnumerator
End Interface ' IProduct
Public Class Product
Implements IProduct
Dim productList As Collection = New Collection
Private m_name As String = ""
Private m_num As Integer = 0
Private m_cost As Integer = 0
Public Sub New(ByVal name As String, ByVal num As Integer, ByVal cost As Integer)
m_name = name
m_num = num
m_cost = cost
End Sub
'''
''' <param name="cost"></param>
''' <param name="num"></param>
''' <param name="name"></param>
Public Sub add(ByVal cost As Integer, ByVal num As Integer, ByVal name As String) Implements IProduct.add
If productList Is Nothing Then
productList = New Collection
End If
productList.Add(New Product(name, num, cost))
End Sub
Public Function getProjectInfo() As String Implements IProduct.getProjectInfo
Dim info As String = ""
info = info & "项目名称是:" & m_name
info = info & "项目人数是:" & m_num
info = info & "项目费用是:" & m_cost
Return info
End Function
Public Function GetEnumerator() As System.Collections.IEnumerator Implements IProduct.GetEnumerator
'Return New ProjectEnumer(productList).GetEnumerator
Return productList.GetEnumerator
End Function
End Class ' Product
Module Client
Sub Main()
Dim _iproduct As IProduct = New Product("广州所有项目", 300, 10000)
_iproduct.add(1000, 1, "超人项目")
_iproduct.add(10000, 10, "政府项目")
Dim list As System.Collections.IEnumerator = _iproduct.GetEnumerator
While (list.MoveNext)
Console.WriteLine(CType(list.Current, IProduct).getProjectInfo())
End While
Console.Read()
End Sub
End Module

标签:End,14,迭代,GetEnumerator,ByVal,list,设计模式,name
From: https://blog.51cto.com/u_15870687/5837356

相关文章

  • 设计模式利剑17-门面模式
    定     义:要求一个子系统的外部与其内部的通信必须通过一个统一的对象进行,门面模式提供一个高层次的接口,使得子系统更易于使用优     点:           ......
  • 设计模式利剑22--享元模式
    定     义:使用共享对象可以有效地支持大量的细粒度的对象,享元模式可以避免大量非常相似类的开销。在程序设计中有时需要生成大量细             粒度的......
  • 强化学习代码实战-03动态规划算法(策略迭代)
    #获取一个格子的状态defget_state(row,col):ifrow!=3:return'ground'ifrow==3andcol==11:return'terminal'ifrow==3......
  • 洛谷P1434滑雪分析
    [SHOI2002]滑雪题目描述Michael喜欢滑雪。这并不奇怪,因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来......
  • js设计模式-类式继承
    js中怎么实现类式继承呢?首先声明父类,在声明子类,然后将父类的实例赋予给子类的原型就可以了。假设父类长这样:varBook=function(id,name,price){//私有的属性......
  • 设计模式汇总
    基础​​UML各种图总结​​​​依赖、关联、聚合和组合之间的区别​​设计模式的原则​​设计模式七大原则​​创建型​​简单工厂模式​​​​工厂方法模式​​​​抽象工......
  • 【BUG记录】 SSL: error:140AB18F:SSL routines:SSL_CTX_use_certificate:ee key too
    在为nginx添加SSL认证的时候,出现了如下错误nginx:[emerg]SSL_CTX_use_certificate("/ssl/server.crt")failed(SSL:error:140AB18F:SSLroutines:SSL_CTX_use_certifica......
  • java 迭代器使用
    原文链接:https://blog.csdn.net/ACE_kk/article/details/126182500一、前言在迭代器(Iterator)没有出现之前,如果要遍历数组和集合,需要使用方法。数组遍历,代码如下:String[......
  • day14 --> (HTTP协议响应消息、Response对象、ServletContext对象)
    HTTP协议:1、请求消息:客户端发送给服务器端的数据数据格式:1.请求行2.请求头3.请求空行4.请求体2、响应消息:服务器端发送给客户端的数据数据格式:1.响应行1、组成:协......
  • php 设计模式
    <?php//协程生成器函数并发classTest{publicfunctionasync(){$start=microtime(true);$url="https://money.finance.sina.com.c......