Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visitor Matt G. Ellis. Intent Metsker: Let developers define a new operation for a hierarchy without changing the hierarchy classes. GoF: Represent an.

Similar presentations


Presentation on theme: "Visitor Matt G. Ellis. Intent Metsker: Let developers define a new operation for a hierarchy without changing the hierarchy classes. GoF: Represent an."— Presentation transcript:

1 Visitor Matt G. Ellis

2 Intent Metsker: Let developers define a new operation for a hierarchy without changing the hierarchy classes. GoF: Represent an operation to be preformed 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

3 Visitor by Example - UML

4 Visitor by Example – Accessibility One aspect of allowing for later extensions is that the hierarchy classes make their attributes accessible. getId() method in MachineComponent and getComponents() in MachineComposite provide access to critical attributes of hierarchy.

5 Visitor by Example – Supporting Visitor To support Visitor, each class in the hierarchy must have a accept method. This is defined abstractly in the parent class, but the children implement it the same way:

6 Challenge 29.1 Why can’t we move the definition of the accept method up to the parent class, if it is the same in all the subclasses? Hint: What difference will a Java compiler see between the accept() methods in the Machine and MachineComposite classes?

7 Visitor by Example - Interface The accept methods in the Class hierarchy along with this interface allow developers to provide new operations on the hierarchy.

8 Visitor by Example – The Irish You’ve been given the task to bring a new Factory in Dublin online. For some reason you want a representation of the factory in a tree.

9 Visitor by Example – The Irish Suppose that you need to find a particular machine within the factory model. To add this ability without modifying the MachineComponent hierarchy, you can create a FindVisitor class.

10 Visitor by Example – FindVisitor The FindVisitor effectively adds a find() method to the MachineComponent hierarchy, without touching the code! The visit() methods do not return an object, so the FindVisitor class records the status of a search in its sought instance variable.

11 Visitor by Example – FindVisitor

12 Visitor by Example – Accept/Visit Visitor doesn’t need to know about the type of the underlying classes. Accept asks the class to call the Visit method, which gets the type from the class being visited.

13 Visitor by Example – RakeVisitor Let’s say we want to find all the leaf nodes – machines – in a machine component, how do we do this? We use a Visitor, of course!

14 Challenge 29.3 – RakeVisitor

15

16 Visitor by Example - Disclaimer The FindVisitor and RakeVisitor classes each add a new behavior to the MachineComponent hierarchy. A danger in writing visitors is that they require an understanding of the hierarchy that you are extending. A change in the hierarchy may break your visitor, and you may misunderstand the mechanics of the hierarchy initially. In particular, you may have to handle cycles if the composite you are visiting does not prevent them.

17 Writing a Pretty Printer with Visitor Using Iterator we were able to create an ad-hoc pretty printer, but with Visitor we can do a better job.

18 Pretty Printer – Old vs. New

19 Pretty Printer - Setup

20

21 Pretty Printer – Visit Methods

22 Pretty Printer – Print Composite

23 Pretty Printer – Print Tag

24 The Great Visitor Debate Some argue Visitor is fragile  Adding new subclasses to the hierarchy can break the Visitor  The developer of the Visitor needs to understand the underlying framework (for example, they might need to avoid loops) Other’s argue that it is useful  Developers offer ways to strengthen the pattern, but often with added complexity

25 A Classical Use Visitors are commonly used when developing compilers to work on Abstract Syntax Trees generated by a parser. However, in this case, the visited class has little or no behavior, all the behavior is handled by the visiting class

26 Challenge 29.5 (The Last One) Like any pattern, Visitor is never necessary; if it were, it would automatically appear everywhere it was needed. For Visitor, though, alternatives may provide a sturdier design. List two alternatives to building Visitor into the Oozinoz machine and process hierarchies.


Download ppt "Visitor Matt G. Ellis. Intent Metsker: Let developers define a new operation for a hierarchy without changing the hierarchy classes. GoF: Represent an."

Similar presentations


Ads by Google