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

Design Pattern --------------------- Iterator

    博客分类:
  • Base
阅读更多
好文借鉴
引用
对容器中元素的访问涉及到3个方面。
1.容器的类型
2.检索容器内元素的方法
3.对元素的操作
比如说我们有一个表示书店的book_store类。里面保存了各种各样的book类的实例。
book类有name和type两种属性。表示书的名字和类别。
因此book_store类内部会用一个容器来保存book的实例。比如list。
class book
{
public:
string name;
string type;
};

class book_store
{
private:
list<book> m_books;
};

我们现在有这样一个简单的应用,那就是输出所有的书名到屏幕。
那么我们来看一下在引入Iterator模式前有那几种实现方法,各有什么缺点。
1.给book_store类增加一个print_book_name的函数。来实现这个功能。
这样做的缺点有两个:
(1).将输入输出逻辑和业务对象绑定在一起,导致了今后系统难以变更。
(2).输出逻辑和内部实现绑定。
比如现在要把输出到屏幕改成输出到log文件的话,那么就需要修改print_book_name了。
2.给book_store类增加一个get_list函数。然后写一个print_book类来负责打印书名的列表。这是一个初学者常用的方式。表面上看来似乎两个类各担其责,一个表示业务对象一个负责打印。
如果需要打印到log文件的话,只要新增加一个log_book类,book_store和print_book都不受影响。解决了上面的这个问题。
这样做的缺点是:
(1).把book_store类的内部实现给暴露出来了,违反了封装的原则。
如果现在把内部容器类型从list换成了vector的话,就要修改输出逻辑了。也就是说,要同时改写print_book类和log_book类。
3.增加一个list和vector类共同的基类/接口,比如I_container。然后给book_store类增加一个get_books函数,返回I_container。
这在一定程度上解决了上面的问题。但是并不彻底。应为还是暴露了内部的实现,只不过从list上升到了I_container。
· 如果现在系统发生了变化,book_store不再在本地保存books了,而是需要通过网络取得的话,print_book和log_book就无法对应了。
· 另一个限制是,我需要一个新的机能,那就是打印所有type为”烹饪书”的机能的话,就需要一个print_cook_book类了,而这里边的格式化代码和输出代码和print_book是相同的。重复代码是维护的噩梦。

接下来我们看一下Iterator模式如何解决以上的这些问题。
首先,我们引入一个I_iterator的Interface。
然后创建一个I_iterator的实现类all_book_iterator。这个类可以迭代book_store中的所有book。
接下来创建一个print_book类,从I_iterator中取得每一个book,然后打印到屏幕。

下面我们看一下如何对应上面的这些需求变更。
1.要求输出到log文件
追加一个log_book类,其他都类都不需要改变。
2.将内部容器从list变成vector。
修改iterator中的相关代码。Iterator的使用者不会受到影响。
3.从网络取得数据。
修改iterator中的相关代码。Iterator的使用者不会受到影响。
4.按照type检索
在iterator中添加一个type的属性,或者是构造一个新的type_search_iterator。
在iterator中把不符合检索条件的book过滤掉。
5.按照某一特定顺序输出book名到屏幕,或是log文件。
实现一个按该顺序输出的iterator。
6.以同样的方式打印cd_store。
实现对应于cd_store的iterator。

可见,iterator模式在各种iterator中封装了检索元素的方法。
将容器和对容器中元素的操作完全隔开。
大大增加了代码的可重用性。
分享到:
评论

相关推荐

    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 -- ...

    36种最新设计模式整理

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

    DesignPattern : Iterator

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

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

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

    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 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

    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.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 pattern

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

    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章介绍...

    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 ...

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

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

    LeetCode最全代码

    * [Design](https://github.com/kamyu104/LeetCode#design) ## Database * [SQL](https://github.com/kamyu104/LeetCode#sql) ## Shell * [Shell Script](https://github.com/kamyu104/LeetCode#shell-script)...

    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, ...

    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,...

Global site tag (gtag.js) - Google Analytics