能看建设动漫黄图的网站,快刷网站,如何做一个好网站,大蒜做营销型网站文章目录 1. 概念1.1 角色1.2 类图 2. 代码示例2.1 需求2.2 代码2.3 类图 1. 概念
迭代器#xff08;Iterator#xff09;能够在不暴露聚合体内部表示的情况下#xff0c;向客户端提供遍历聚合元素的方法。
1.1 角色
InterfaceAggregate#xff08;抽象聚合#xff09;… 文章目录 1. 概念1.1 角色1.2 类图 2. 代码示例2.1 需求2.2 代码2.3 类图 1. 概念
迭代器Iterator能够在不暴露聚合体内部表示的情况下向客户端提供遍历聚合元素的方法。
1.1 角色
InterfaceAggregate抽象聚合定义存储、添加、删除聚合元素以及创建迭代器对象的接口ConcreteAggregate具体聚合实现抽象聚合类。它的方法可以返回一个具体迭代器的实例Iterator抽象迭代器定义访问和遍历聚合元素的接口通常包含 hasNext()、next() 等方法Concretelterator具体迭代器实现抽象迭代器接口中所定义的方法完成对聚合对象的遍历记录遍历的当前位置。
1.2 类图 #mermaid-svg-80tUvmPRCgZrLjMc {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-80tUvmPRCgZrLjMc .error-icon{fill:#552222;}#mermaid-svg-80tUvmPRCgZrLjMc .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-80tUvmPRCgZrLjMc .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-80tUvmPRCgZrLjMc .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-80tUvmPRCgZrLjMc .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-80tUvmPRCgZrLjMc .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-80tUvmPRCgZrLjMc .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-80tUvmPRCgZrLjMc .marker{fill:#333333;stroke:#333333;}#mermaid-svg-80tUvmPRCgZrLjMc .marker.cross{stroke:#333333;}#mermaid-svg-80tUvmPRCgZrLjMc svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-80tUvmPRCgZrLjMc g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-80tUvmPRCgZrLjMc g.classGroup text .title{font-weight:bolder;}#mermaid-svg-80tUvmPRCgZrLjMc .nodeLabel,#mermaid-svg-80tUvmPRCgZrLjMc .edgeLabel{color:#131300;}#mermaid-svg-80tUvmPRCgZrLjMc .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-80tUvmPRCgZrLjMc .label text{fill:#131300;}#mermaid-svg-80tUvmPRCgZrLjMc .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-80tUvmPRCgZrLjMc .classTitle{font-weight:bolder;}#mermaid-svg-80tUvmPRCgZrLjMc .node rect,#mermaid-svg-80tUvmPRCgZrLjMc .node circle,#mermaid-svg-80tUvmPRCgZrLjMc .node ellipse,#mermaid-svg-80tUvmPRCgZrLjMc .node polygon,#mermaid-svg-80tUvmPRCgZrLjMc .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-80tUvmPRCgZrLjMc .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-80tUvmPRCgZrLjMc g.clickable{cursor:pointer;}#mermaid-svg-80tUvmPRCgZrLjMc g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-80tUvmPRCgZrLjMc g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-80tUvmPRCgZrLjMc .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-80tUvmPRCgZrLjMc .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-80tUvmPRCgZrLjMc .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-80tUvmPRCgZrLjMc .dashed-line{stroke-dasharray:3;}#mermaid-svg-80tUvmPRCgZrLjMc #compositionStart,#mermaid-svg-80tUvmPRCgZrLjMc .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-80tUvmPRCgZrLjMc #compositionEnd,#mermaid-svg-80tUvmPRCgZrLjMc .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-80tUvmPRCgZrLjMc #dependencyStart,#mermaid-svg-80tUvmPRCgZrLjMc .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-80tUvmPRCgZrLjMc #dependencyStart,#mermaid-svg-80tUvmPRCgZrLjMc .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-80tUvmPRCgZrLjMc #extensionStart,#mermaid-svg-80tUvmPRCgZrLjMc .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-80tUvmPRCgZrLjMc #extensionEnd,#mermaid-svg-80tUvmPRCgZrLjMc .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-80tUvmPRCgZrLjMc #aggregationStart,#mermaid-svg-80tUvmPRCgZrLjMc .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-80tUvmPRCgZrLjMc #aggregationEnd,#mermaid-svg-80tUvmPRCgZrLjMc .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-80tUvmPRCgZrLjMc .edgeTerminals{font-size:11px;}#mermaid-svg-80tUvmPRCgZrLjMc :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Client «interface» Iterator HasNext() : bool First() IsDone() Next() : interface CurrentItem() «interface» Aggregate CreateIterator() : Iterator ConcreteIterator HasNext() : bool First() IsDone() Next() : interface CurrentItem() ConcreteAggregate CreateIterator() : Iterator 2. 代码示例
2.1 需求
实例化一个具体聚合创建一个迭代器实例用迭代器遍历这个聚合。 迭代器通常还会有查看首元素、尾元素、指针位置等方法有兴趣可以自己试一下这个简单示例里不写了。 2.2 代码
代码
package mainimport fmt// 定义抽象迭代器
type Iterator interface {HasNext() boolNext() interface{}
}// 定义具体迭代器
type ConcreteIterator struct {//它关联具体聚合aggregate *ConcreteAggregateindex int
}// 定义方法查看迭代是否结束
func (i *ConcreteIterator) HasNext() bool {return i.index len(i.aggregate.items)
}// 定义方法返回下一个节点
func (i *ConcreteIterator) Next() interface{} {if i.HasNext() {item : i.aggregate.items[i.index]i.indexreturn item}return nil
}// 定义抽象聚合
type Aggregate interface {CreateIterator() IteratorAddItem(item interface{})
}// 定义具体聚合
type ConcreteAggregate struct {items []interface{}
}// 定义方法创建迭代器
func (a *ConcreteAggregate) CreateIterator() Iterator {return ConcreteIterator{aggregate: a}
}// 定义方法添加item为了测试方便和迭代器方法无关
func (a *ConcreteAggregate) AddItem(item interface{}) {a.items append(a.items, item)
}func main() {//实例化一个聚合加入三个Item他们的类型可以不同var aggregate Aggregateaggregate ConcreteAggregate{}aggregate.AddItem(Item 1)aggregate.AddItem(2)aggregate.AddItem(Item 3)//实例化一个迭代器iterator : aggregate.CreateIterator()//验证结果for iterator.HasNext() {item : iterator.Next()fmt.Println(item)}
}输出
Item 1
2
Item 32.3 类图 #mermaid-svg-gvVKNcs2TFexEK71 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-gvVKNcs2TFexEK71 .error-icon{fill:#552222;}#mermaid-svg-gvVKNcs2TFexEK71 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-gvVKNcs2TFexEK71 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-gvVKNcs2TFexEK71 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-gvVKNcs2TFexEK71 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-gvVKNcs2TFexEK71 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-gvVKNcs2TFexEK71 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-gvVKNcs2TFexEK71 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-gvVKNcs2TFexEK71 .marker.cross{stroke:#333333;}#mermaid-svg-gvVKNcs2TFexEK71 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-gvVKNcs2TFexEK71 g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-gvVKNcs2TFexEK71 g.classGroup text .title{font-weight:bolder;}#mermaid-svg-gvVKNcs2TFexEK71 .nodeLabel,#mermaid-svg-gvVKNcs2TFexEK71 .edgeLabel{color:#131300;}#mermaid-svg-gvVKNcs2TFexEK71 .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-gvVKNcs2TFexEK71 .label text{fill:#131300;}#mermaid-svg-gvVKNcs2TFexEK71 .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-gvVKNcs2TFexEK71 .classTitle{font-weight:bolder;}#mermaid-svg-gvVKNcs2TFexEK71 .node rect,#mermaid-svg-gvVKNcs2TFexEK71 .node circle,#mermaid-svg-gvVKNcs2TFexEK71 .node ellipse,#mermaid-svg-gvVKNcs2TFexEK71 .node polygon,#mermaid-svg-gvVKNcs2TFexEK71 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-gvVKNcs2TFexEK71 .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-gvVKNcs2TFexEK71 g.clickable{cursor:pointer;}#mermaid-svg-gvVKNcs2TFexEK71 g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-gvVKNcs2TFexEK71 g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-gvVKNcs2TFexEK71 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-gvVKNcs2TFexEK71 .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-gvVKNcs2TFexEK71 .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-gvVKNcs2TFexEK71 .dashed-line{stroke-dasharray:3;}#mermaid-svg-gvVKNcs2TFexEK71 #compositionStart,#mermaid-svg-gvVKNcs2TFexEK71 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gvVKNcs2TFexEK71 #compositionEnd,#mermaid-svg-gvVKNcs2TFexEK71 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gvVKNcs2TFexEK71 #dependencyStart,#mermaid-svg-gvVKNcs2TFexEK71 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gvVKNcs2TFexEK71 #dependencyStart,#mermaid-svg-gvVKNcs2TFexEK71 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gvVKNcs2TFexEK71 #extensionStart,#mermaid-svg-gvVKNcs2TFexEK71 .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gvVKNcs2TFexEK71 #extensionEnd,#mermaid-svg-gvVKNcs2TFexEK71 .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gvVKNcs2TFexEK71 #aggregationStart,#mermaid-svg-gvVKNcs2TFexEK71 .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gvVKNcs2TFexEK71 #aggregationEnd,#mermaid-svg-gvVKNcs2TFexEK71 .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gvVKNcs2TFexEK71 .edgeTerminals{font-size:11px;}#mermaid-svg-gvVKNcs2TFexEK71 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Client «interface» Iterator HasNext() : bool Next() : interface «interface» Aggregate CreateIterator() : Iterator AddItem(item:interface) ConcreteIterator *ConcreteAggregate aggregate Int index HasNext() : bool Next() : interface ConcreteAggregate []interface items CreateIterator() : Iterator GetItem(index int) : interface AddItem(item interface)