Skip to Content
面向对象的思考过程(原书第5版)
book

面向对象的思考过程(原书第5版)

by 马特 魏斯费尔德
August 2021
Intermediate to advanced
450 pages
4h 46m
Chinese
Pearson
Content preview from 面向对象的思考过程(原书第5版)

10.3.3 行为型模式

行为型模式包含以下类别:

·责任链模式

·命令模式

·解释器模式

·迭代器模式

·中介者模式

·备忘录模式

·观察者模式

·状态模式

·策略模式

·模板方法模式

·访问者模式

我们将迭代器模式作为行为型模式类别中的示例。这是一个非常常见的模式,多种编程语言都实现了该模式。

迭代器设计模式

迭代器提供了一个标准的机制来遍历集合(比如列表)。该机制提供的功能要保证集合中的每个条目都必须至少被访问一次。迭代器模式隐藏了信息,保证了集合的内部结构是严密的。迭代器模式也规定可以创建多个迭代器,迭代器之间不会相互妨碍。Java语言自身实现了迭代器。以下的代码创建了一个列表,并且插入一组字符串:

然后创建了一个枚举类型,从而可以通过该枚举进行迭代。方法iterate()用来执行迭代功能。在这个方法中,我们使用Java枚举方法hasMoreElements(),该方法会遍历这个向量并且列出所有名字。

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Node与Express开发(第2版)

Node与Express开发(第2版)

Ethan Brown
计算机科学导论:跨学科方法

计算机科学导论:跨学科方法

罗伯特 塞奇威克, 凯文 韦恩
手把手教会你linux

手把手教会你linux

桑德.范.乌格特
Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉

Publisher Resources

ISBN: 9787111684527