Download - Visitor Pattern

Transcript
Page 1: Visitor Pattern

S

Visitor Pattern

Yateen L. Nikharge

Aravindh Manickavasagam

Ider Zheng

Page 2: Visitor Pattern

The VISITOR Family

The Visitor family allows new methods to be addedto existing hierarchies without modifying thehierarchies.

The Patterns in this family are:

S VISITOR

S ACYCLIC VISITOR

S DECORATOR

S EXTENSION OBJECT

Page 3: Visitor Pattern

Intent

S Represent an operation to be performed on the elements of an object structure.

S Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Page 4: Visitor Pattern

Motivation

S Decouple the data structure and algorithms.

S Allow addition of new algorithms without changing the data structure.

S Example: Modem Configuration

S To configure a modem for Unix, create an instance of the visitor and pass it to accept.

S The appropriate derivative call visit(this)

S New OS configuration can be added by adding a new derivative of visitor.

Page 5: Visitor Pattern

Motivation (contd…)

Page 6: Visitor Pattern

Motivation(contd…)

Page 7: Visitor Pattern

Applicability

Use the pattern when-

S An object structure contains many classes of objects with differing interfaces…

S Many distinct and unrelated operations need to be performed on objects in an object structure…

S The classes defining the object structure rarely change, but you often want to define new operations over the structure…

Page 8: Visitor Pattern

Structure

Page 9: Visitor Pattern

Participants

S Visitor (ModemVisitor): Declares a Visit operation or each class…

S ConcreteVisitor (ConfigureUnixVisitor):

Implements each Visit…

S Element (Modem) :Defines an Accept operation…

S ConcreteElement(ZoomModem): Implements Accept …

Page 10: Visitor Pattern

Participants(Contd…)

S ObjectStructure (Program):

May provide a high-level interface to allow the visitor to visit its

elements .

Page 11: Visitor Pattern

Collaborations

S A client using the Visitor pattern must create a ConcreteVisitor

object and traverse the object structure, visiting each element

with the visitor.

S When an element is visited, it calls the visitor operation that

corresponds to its class. The element supplies itself as an

argument to this operation to let the visitor access its state, if

necessary.

Page 12: Visitor Pattern

Collaborations cont.

Page 13: Visitor Pattern

Consequences-Benefits

S Adding new operations

S Gathers related operations and separates unrelated ones

S Visitors can visit objects that don’t have a common parent

class

S Visitors can accumulate state as they visit each element

Page 14: Visitor Pattern

Consequences - Liabilities

S Adding new ConcreteElement classes is hard

S Allowing a Visitor to access the internal state of a

Concrete Element breaks encapsulation

Page 15: Visitor Pattern

Implementation - Issues

S Double Dispatch

S Who is Responsible for Traversing the Object Structure?

S Object Structure

S Visitor – whenever operations depend on other operations

on the object structure

S Iterator Object

Page 16: Visitor Pattern

Sam

ple

Cod

es

Page 17: Visitor Pattern

Implementation

Function Pointer

Page 18: Visitor Pattern
Page 19: Visitor Pattern

Implementation

Global Function

Page 20: Visitor Pattern

Implementation

Static Methods

Functor

Page 21: Visitor Pattern

Implementation

Graphic Project

From CSE 687 Objected Oriented Design

Page 22: Visitor Pattern

Graphic

“v1” “v3”

“v4” “v7”

“v2”

“v5”

“v6”“e8”

“e3”

“e6”

“e7”“e4”

“e1”

“e5”

“e9”“e2”

Page 23: Visitor Pattern

Known Uses

S Smalltalk-80 Compiler

S IRIS Inventor Toolkit

S X Consortium’s Fresco Application Toolkit

S Bistro Programming Language Compiler

Page 24: Visitor Pattern

Related Patterns

S Composite

S Interpreter

Page 25: Visitor Pattern

References

S Design Patterns: Elements of Reusable Object-Oriented

Software

By Erich Gamma, Richard Helm, Ralph

Johnson, and John Vlissides

Shttp://www.codeproject.com/KB/aspnet/SoftArch3.aspx


Top Related