visitor
play

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


  1. Visitor 1

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

  3. Some claim that Visitor is a misleading name for this design pattern.  Performing an operation on elements in an object 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 operation on each element. 3

  4. A file system has many element types, and many operations that you might like to perform on it. mydir  Basic Types directory • Directories • Text files subdir1 subdir2 • Image files directory directory • Video files • Sound files text1 subsubdir image2 sound2  Operations directory jpeg text mp3 • Number of files • Total size image1 video1 • Newest text file mp4 gif • Resize images 4

  5. There are a number of considerations to do a good OO design of this subsystem.  Operations mydir directory • Number of files • Total size • Newest text file subdir1 subdir2 directory directory • Resize images text1 subsubdir image2 sound2 directory jpeg text mp3 What processing steps will these operations have in common? image1 video1 mp4 gif How should the operations and information be encapsulated? What if separation of concerns is most important? 5

  6. The Visitor pattern separates concerns for structure maintenance and doing operations. Client ObjectStructure Visitor visitCEA(CEA a) or visit(CEA a) Element Structure visitCEB(CEB b) or visit(CEB b) accept(Visitor v) Maintenance The Operations ConcreteElementA ConcreteElementB ConcreteVisitorA ConcreteVisitorB operation() operation() visitCEA(CEA a) visitCEA(CEA a) accept(Visitor v) accept(Visitor v) visitCEB(CEB b) visitCEB(CEB b) v.visitConcreteElementA(this) or v.visit(this) v.visitConcreteElementA(this) or v.visit(this) 6

  7. Here is the object structure for the file system example with just two element types. The Object Structure The Operations File FileVisitor FileSystem name() visit(Dir d) size() visit(Text t) accept(FileVisitor v) Dir Text NewestVisitor TotalLinesVisitor size() size() visit(Dir d) visit(Dir d) elements() lineCount() visit(Text t) visit(Text t) accept(FileVisitor v) accept(FileVisitor v) getNewest() getLines() TotalTimeVisitor visit(Dir d) v.visit(this) v.visit(this) visit(Text t) getTime() 7

  8. As with most design choices, Visitor is not a completely free lunch without drawbacks. 1. Adding new concrete element classes is difficult: 1. All visitors must change 2. Here putting functionality in structure probably better New Classes Rare Frequent New Ops? Rare Either Structure Frequent Visitor Hard Visitor assumes element interfaces are “powerful enough”. To get the 2. necessary power, you may need to break encapsulation. 1. Friends? 2. Package access? 8

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend