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

Design Patterns -- Visitor

阅读更多

Visitor 模式是这个系列文章(也是笔者在读的这本书)的最后一个模式。按惯例,首先给出定义:Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

总结一下,这里有两点:1。添加operation 2。不改变原有的classes

事实上,到后面我们会发现要实现上面的两点有一个前提,就是原有的classes里必须有一个传入visitor 的函数。(即在开始设计类时,必须为后面的扩展留出一个冗余的函数。当然,这个函数也可以在子类里再添加。)

看一下类图:

 

这张图比较大,从Client 到ObjectStruture 到Element, 我们可以把这三者合起来作为一个已存在的Hierarchy(classes)。然后就可以看到,在加入Visitor 后,通过Accept 方法的调用,使Visitor 得以进入到Element 内部,接收Element(即,将Element 自身的控制权交给Visitor,比如通过:vistor.VisitConcreteElementB(this))

而《Design Pattern Java Workbook》认为,要较好的运用Visitor 模式,一般需要两点:

1。已有的classes (如Element )使其属性可访问(public )。

2。在Element 内部添加类似Accept 的函数并定义Visitor 。

另外,说到Visitor 模式,不可避免的要说一下 "double dispatch".  假设在Vistor 中有方法accept ( visitorInterface), Client 中有方法visit ( clientInterface). (请注意,这里说visitorInterface 和clientInterface, 意味着继承他们的各有不同的Visitor 和Client. )如图:

。。。(图没法传)

该顺序图大意为:首先Visitor 调用Client 的Accept ( this); 接着Client 的Accept 方法中调用传入Visitor 的 visit (this)。通过着简单的两步,保证了动态调用的 Visitor 是本Client 对应的Vistor。

“double dispatch“ 是Vistor 中比较重要的一点。另外,当Visitor 需要访问的Hierarchy 是一个比较复杂的结构(比如Composite)时,情况也需要更详细的处理。

这里只是简单介绍一些基本的东西,所以上述的内容不做描述,如果有读者有兴趣可以参考给出的参考文献。

 

参考:

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

http://www.crackj2ee.com/Article/ShowArticle.asp?ArticleID=227(笔者认为此文内容有误,仅供参考)

http://www.cs.rice.edu/~cork/Book/node62.html

http://www.cnblogs.com/zhenyulu/articles/79719.html

http://www.cjsdn.net/post/view?bid=17&id=180748&sty=3&age=0 

http://www.cnblogs.com/idior/archive/2006/02/03/325036.html

分享到:
评论

相关推荐

    Packt.Go.Design.Patterns.2017

    Behavioral Patterns - Visitor, State, Mediator, and Observer Design Patterns Chapter 8. Introduction to Gos Concurrency Chapter 9. Concurrency Patterns - Barrier, Future, and Pipeline Design Patterns...

    Design.Patterns.Explained.Simply

    We've tried hard to avoid both of these categories with Design Patterns Explained Simply. This book is fast and simple way to get the idea behind each of the 29 popular design patterns. The book is ...

    Design Patterns Elements of Reusable Object-Oriented Software

    • How Design Patterns Solve Design Problems • How to Select a Design Pattern • How to Use a Design Pattern A Case Study: Designing a Document Editor • Design Problems • Document Structure ...

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

    ■Chapter 9: Behavioral Patterns: State, Strategy, Template Method, and Visitor ■Chapter 10: Patterns of Enterprise Application Architecture: Repository, Unit of Work, Lazy Load, and Service Layer ...

    Apress.Pro.Design.Patterns.in.Swift

    Pro Design Patterns in Swift shows you how to harness the power and flexibility of Swift to apply the most important and enduring design patterns to your applications, taking your development ...

    Modern C++ Design Generic Programming and Design Patterns Applied.pdf

    Patterns such as Visitor, Singleton, Command, and Factories Multi-method engines For each generic component, the book presents the fundamental problems and design options, and finally implements a ...

    Data Structures And Algorithms With Object-Oriented Design Patterns In C Sharp.pdf

    the emerging object-oriented design patterns. Experienced object-oriented programmers find that certain ways of doing things work best and that these ways occur over and over again. The book shows how...

    《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 ...Chapter 23: Visitor Patterns Chapter 24: Interpreter Patterns

    design patterns elements of reusable object-oriented software

    Bridge(桥接)、Builder(生成器)、Chainof Responsibility(责任链)、Flyweight(蝇量)、Interpreter(解释器)、Mediator(中介者)、Memento(备忘录)、Prototype(原型)、Visitor(访问者)。 另外,第1章...

    Head First Design Patterns 高清英文版

    至于第14章则很快地浏览尚未介绍的设计模式,包括Bridge、Builder、Chain of Responsibility、Flyweight、Interpreter、Mediator、Memento、Prototype,Visitor。第1章还介绍了四个○○基本概念(抽象、封装、继承、...

    Head First Design Patterns

    Flyweight、Interpreter、Mediator、Memento、Prototype、Visitor。第1章还介绍了四个OO基本概念(抽象、封装、继承、多态),而第1章到第9章也陆续介绍了九个OO原则(Principle)。 千万不要轻视这些OO原则,因为...

    Head First Design Patterns 英文版 Head First设计模式

    《Head First Design Patterns》编辑推荐:强大的写作阵容。《Head First Design Patterns》作者Eric Freeman;ElElisabeth Freeman是作家、讲师和技术顾问。Eric拥有耶鲁大学的计算机科学博士学位,E1isabath拥有...

    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 ABOUT DESIGN PATTERNS SINGLETON PATTERN FACTORY PATTERN ABSTRACT ...

    head first design patterns 英文原版高清

    至于第14章则很快地浏览尚未介绍的设计模式,包括Bridge、Builder、ChainofResponsibility、Flyweight、Interpreter、Mediator、Memento、Prototype,Visitor。第1章还介绍了四个基本概念(抽象、封装、继承、多态),...

    typescript-design-patterns:TypeScript中的设计模式

    TypeScript Design Patterns 该项目基于 TypeScript 实现了下列设计模式: Creational(创建型模式) Structural Patterns(结构型模式) Bridge Composite Behavioral Patterns(行为型模式) Command Interpreter ...

    designPatterns:设计模式

    design pattern 设计模式 创建型设计模式 创建型模式设计到将对象实例化,这类模式都提供了一个方法,将客户从所需要的实例化对象中解耦。 原型模式 Prototype 结构型设计模式 结构型模式可以让你把类或者对象组合到...

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

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

    设计模式(Design.Patterns.CHN)

    2.8.7 Visitor 类及其子类 51 2.8.8 Visitor 模式 52 2.9 小结 53 第3章 创建型模式 54 3.1 Abstract Factory(抽象工厂)— 对象创建型模式 57 3.2 Builder(生成器)—对象创建型 模式 63 3.3 Factory Method...

    C-Sharp-Behavioral-Design-Patterns:行为设计模式的例子

    责任链一种在一系列对象之间传递请求的方式命令将命令请求封装为对象解释器一种在程序中包含语言元素的方法迭代器顺序访问集合的元素介体定义类...方法将算法的确切步骤推迟到子类访客(Visitor)为类定义一个新操作...

    [Java设计模式(第2版)(Design.Patterns.in.Java).John.Metsker

    《java设计模式(第2版)》通过一个完整的java项目对经典著作design patterns一书介绍的23种设计模式进行了深入分析与讲解,实践性强,却又不失对模式本质的探讨。本书创造性地将这些模式分为5大类别,以充分展现各个...

Global site tag (gtag.js) - Google Analytics