Visitor 1 Visitor Intent Represent an operation to be performed on - - PowerPoint PPT Presentation

visitor
SMART_READER_LITE
LIVE PREVIEW

Visitor 1 Visitor Intent Represent an operation to be performed on - - PowerPoint PPT Presentation

Visitor 1 Visitor Intent 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. (Behavioral) 2 Some claim


slide-1
SLIDE 1

1

Visitor

slide-2
SLIDE 2

2

Visitor Intent 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. (Behavioral)

slide-3
SLIDE 3

3

  • Performing an operation on elements in an
  • bject structure is the primary purpose for

Visitor.

  • If you think of performing the operation on a

single element the Visitor name may seem unusual.

  • The operation is typically performed on the

entire object structure by having a concrete visitor “visit” each element to perform the

  • peration on each element.

Some claim that Visitor is a misleading name for this design pattern.

slide-4
SLIDE 4

4

A file system has many element types, and many

  • perations that you might like to perform on it.
  • Basic Types
  • Directories
  • Text files
  • Image files
  • Video files
  • Sound files
  • Operations
  • Number of files
  • Total size
  • Newest text file
  • Resize images

mydir subdir1 subdir2 image1 video1 subsubdir text1 sound2 image2

directory directory directory directory mp4 gif jpeg text mp3

slide-5
SLIDE 5

5

There are a number of considerations to do a good OO design of this subsystem.

  • Operations
  • Number of files
  • Total size
  • Newest text file
  • Resize images

mydir subdir1 subdir2 image1 video1 subsubdir text1 sound2 image2

directory directory directory directory mp4 gif jpeg text mp3

What processing steps will these

  • perations have in common?

How should the operations and information be encapsulated? What if separation of concerns is most important?

slide-6
SLIDE 6

6

The Visitor pattern separates concerns for structure maintenance and doing operations.

Visitor visitCEA(CEA a) or visit(CEA a) visitCEB(CEB b) or visit(CEB b) ConcreteVisitorA

visitCEA(CEA a) visitCEB(CEB b)

Element accept(Visitor v) ConcreteElementB

  • peration()

accept(Visitor v) v.visitConcreteElementA(this)

  • r v.visit(this)

v.visitConcreteElementA(this)

  • r v.visit(this)

The Operations

ObjectStructure ConcreteVisitorB

visitCEA(CEA a) visitCEB(CEB b)

ConcreteElementA

  • peration()

accept(Visitor v)

Structure Maintenance

Client

slide-7
SLIDE 7

7

Here is the object structure for the file system example with just two element types.

FileVisitor visit(Dir d) visit(Text t) NewestVisitor visit(Dir d) visit(Text t) getNewest() TotalLinesVisitor visit(Dir d) visit(Text t) getLines() File name() size() Dir

size() elements()

Text

size() lineCount()

v.visit(this) v.visit(this)

The Object Structure The Operations

FileSystem accept(FileVisitor v)

accept(FileVisitor v) accept(FileVisitor v)

TotalTimeVisitor visit(Dir d) visit(Text t) getTime()

slide-8
SLIDE 8

8

1. Adding new concrete element classes is difficult: 1. All visitors must change 2. Here putting functionality in structure probably better

As with most design choices, Visitor is not a completely free lunch without drawbacks.

Hard Visitor Frequent Structure Either Rare Frequent Rare New Classes New Ops?

2.

Visitor assumes element interfaces are “powerful enough”. To get the necessary power, you may need to break encapsulation.

1.

Friends?

2.

Package access?