`
dr2tr
  • 浏览: 138468 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Design Pattern -- Iterator

阅读更多

就Java 开发者而言,Iterator 应该是比较容易理解的模式了。 因为用过Java 集合的人会记得有iterator() 这个函数。应用iterator() 返回Iterator 对象,然后对相应的集合元素进行操作,这就是经典的Iterator 的用法。(当然,Iteartor 还涉及另外一个模式,Factory, 这在之前已经讲过)。

在基本的Iterator 模式的定义里是这样讲的:“Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. ” 。 这里有两点:

1。访问聚合的对象( aggregate object),比如集合

2。不暴露内部细节( exposing its underlying representation.)

基本的模式图:

对于这个模式,本身的思想似乎没有什么可说的。但是有几点具体实现还是有必要提一下:

首先是对于Java 开发者,通常可以自定义自己的集合和Iterator 来提前实现集合元素类型转换等操作。在Java 源码中,我们看到通过运用Iterator 和Factory 两种模式,开发者将特定集合类型的Iterator 在该类内作为内部类(inner class) 实现。而当我们开发自己的集合以及Iterator 时,完全也可以这么做

第二,如果自定义记集合内元素是以复合(composite )结构存在的,那么遍历的实现就会有一些复杂。这时的设计可能还要参考Composite 模式的概念。

第三,在Iterate 的过程中,必须关注Thread-Safe 的问题。通常可以通过加互斥锁,或者用Synchronized解决,比如:

list = Collections.synchronizedList(sourceList);
synchronized(list){
...
}

(这里还有一个细节,在Java 中,如果用Iterator遍历操作集合元素,发生线程冲突而程序员没有进行处理的话,Java 会抛出ConcurrentModificationException 异常;而如果不用Iterator, 而只是for 循环,即使有线程问题存在异常也不会被抛出。)

总之,对于Java 开发者而言,Iterator 模式在大部分情况下是现成可用的。而如果要自定义进行开发,有些东西就必须小心了。

参考:

http://www.dofactory.com/Patterns/PatternIterator.aspx

http://squall.cs.ntou.edu.tw/cpp/91spring/Iterator.html

http://www.cnblogs.com/singlepine/archive/2005/10/30/265024.html

http://digi.it.sohu.com/20050508/n240036877.shtml

分享到:
评论

相关推荐

    design-pattern-java.pdf

    自定义语言的实现——解释器模式(五) 自定义语言的实现——解释器模式(六) 迭代器模式-Iterator Pattern 遍历聚合对象中的元素——迭代器模式(一) 遍历聚合对象中的元素——迭代器模式(二) 遍历聚合对象中的...

    Visitor-and-Iterator-Design-Pattern

    您已经提供了一个迭代器类Iterator ,用户可以创建它来遍历整个表达式树。 该迭代器的特殊之处在于它多次访问树的每个节点。 没有子节点的节点将被访问一次。 有一个孩子的节点被访问两次(访问孩子之前和之后)。 ...

    CoreJava-DesignPattern

    CoreJava-DesignPattern 创意设计模式 -- Abstract Factory - Done -- Builder - Done -- Factory Method -- Object Pool -- Prototype - Done -- Singleton - Done 结构设计模式 -- Adapter -- Bridge -- ...

    DesignPattern : Iterator

    NULL 博文链接:https://davyjones2010.iteye.com/blog/1876389

    C++设计模式(Design Pattern)范例源代码

    23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm”供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) 生成...

    36种最新设计模式整理

    Design Pattern: Iterator 模式 Mediator 模式 Memento 模式 Observer 模式 State 模式 Strategy 模式 Template Method 模式 Visitor 模式 Guarded Suspension 模式 Producer Consumer 模式 ...

    Java.Design.Patterns.1537192353

    Java design patterns with the Simplest real world examples which are easy to understand & remember as well. Table of Contents PREFACE ...ITERATOR PATTERN INTERPRETER PATTERN MEMENTO PATTERN

    Design Patterns Elements of Reusable Object-Oriented Software

    • What Is a Design Pattern? • Design Patterns in Smalltalk MVC • Describing Design Patterns • The Catalog of Design Patterns • Organizing the Catalog • How Design Patterns Solve Design ...

    Design.Patterns.Explained.Simply

    If you have ever bought any programming books, you might have noticed that there are two types of them: books that ...Iterator Mediator Memento Null Object Observer State Strategy Template Method Visitor

    《Java Design Patterns》高清完整英文PDF版

    Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....

    Design Patterns in Modern C++--2018

    Reusable Approaches for Object-Oriented... Work with the behavioral patterns such as chain of responsibility, command, iterator, mediator and more Apply functional design patterns such as Monad and more

    设计模式 design pattern

    5.4 ITERATOR(迭代器)—对象行为型 模式 171 5.5 MEDIATOR(中介者)—对象行为型 模式 181 5.6 MEMENTO(备忘录)—对象行为型 模式 188 5.7 OBSERVER(观察者)—对象行为型 模式 194 5.8 STATE(状态)—对象...

    Policy Adaptors and the Boost Iterator Adaptor Library.pdf

    Library is an example of policy-based design and employs template meta-programming. We also present the Policy Adapter implementation pattern, a strategy which can also be used to generate new ...

    design patterns elements of reusable object-oriented software

    ★第1章至第11章陆续介绍了设计模式:Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、TemplatMethod、Iterator、Composite、State、Proxy。 ★第12章介绍...

    Programming.in.the.Large.with.Design.Patterns

    It starts with a general introduction to all types of programming patterns and goes on to describe 10 of the most popular design patterns in detail: Singleton, Iterator, Adapter, Decorator, State, ...

    深入浅出设计模式中文

    Head First Design Pattern 中文版本 1 Welcome to Design Patterns: an introduction 1 2 Keeping your Objects in the know: the Observer Pattern 37 3 Decorating Objects: the Decorator Pattern 79 4 Baking ...

    Beginning SOLID Principles and Design Patterns for ASP.NET Developers.pdf

    ■Chapter 7: Behavioral Patterns: Chain of Responsibility, Command, Interpreter, and Iterator ■Chapter 8: Behavioral Patterns: Mediator, Memento, and Observer ■Chapter 9: Behavioral Patterns: State,...

    oh-my-design-patterns::artist_palette:记录我在学习设计模式时编写的文章和代码

    Oh, my design patterns记录了我学习设计模式时写的文章以及代码。若有纰漏,欢迎指正与交流。:grinning_face:点击查看电子书,体验更好 :backhand_index_pointing_right: 创建型 (Creational)Java Kotlin结构型...

    Object-Oriented-Analysis-Design:面向对象的分析和设计(JAVA语言)设计模式,Java GUI

    面向对象的分析与设计 这些代码是我为Gebze技术大学的CSE443-面向对象的分析和设计课程提交的内容。 家庭作业的主题: HW01: PART1->策略设计模式PART2->观察者设计模式PART3->装饰器设计模式HW02: PART1->单例...

Global site tag (gtag.js) - Google Analytics