Download presentation
Presentation is loading. Please wait.
Published byDwain O’Connor’ Modified over 9 years ago
1
Practical Session 11: The Visitor Pattern
2
Reminder: Polymorphic Variables
3
Animal * + say() * Cat + say() Dog + say() System.out.println(“Miau”) System.out.println(“Woof”) Animal animal; animal = new Dog; animal.say(); \\ “Woof” animal = new Cat; animal.say(); \\ “Miau”
4
What happens if a method accepts an argument? Animal * + say() * + eats(mouse: Mouse) : bool * + eats(cat: Cat): bool * Cat + say() + eats(mouse : Mouse) : bool + eats(cat : Cat) : bool Mouse + say() + eats(mouse : Mouse) : bool + eats(cat : Cat) : bool boolean b; Animal cat = new Cat(); Animal mouse = new Mouse(); b = cat.eats(mouse); //compilation error
5
No support for polymorphism of arguments Java (and many other languages) supports only polymorphism of the receiver (the variable on which the method is invoked). Polymorphism of the receiver is performed using single dispatch. Polymorphism of arguments is performed using multiple-level dispatch, which is resource consuming. Therefore, it is not supported. Nevertheless, we can simulate it using the Visitor Pattern.
6
The visitor pattern (for methods with one argument) Define two interfaces: Visitor and Visited. The Visitor interface contains a method for each possible argument type.
7
The visitor pattern (for methods with one argument) Define two interfaces: Visitor and Visited. The Visitor interface contains a method for each possible argument type. Visitor +visit (cat : Cat) +visit (mouse : Mouse)
8
The visitor pattern (for methods with one argument) Define two interfaces: Visitor and Visited. The Visitor interface contains a method for each possible argument type. The Visited interface contains an accpet() method. Visited +accept(v : Visitor) Visitor +visit (cat : Cat) +visit (mouse : Mouse)
9
The reciever’s base class implements the visitor interface The argument’s base class implements the visited interface The visitor pattern (cont.) (for methods with one argument)
10
The reciever’s base class implements the visitor interface The argument’s base class implements the visited interface The visitor pattern (cont.) (for methods with one argument) Animal * +eats(animal: Animal)* +visit (cat : Cat)* +visit (mouse : Mouse)* Visitor +visit (cat : Cat) +visit (mouse : Mouse)
11
The reciever’s base class implements the visitor interface The argument’s base class implements the visited interface The visitor pattern (cont.) (for methods with one argument) Visited +accept (v : Visitor) Visitor +visit (cat : Cat) +visit (mouse : Mouse) Animal * +eats (animal: Animal)* +accept (v : Visitor)* +visit (cat : Cat)* +visit (mouse : Mouse)*
12
Implementation of Visitor subclasses public class Cat extends Animal{ public void eats(Animal animal){ animal.accept(this); } public void visit(Cat cat){ s.o.p(“No”); } public void visit(Mouse mouse){ s.o.p(“Yes”); } … Implementation of Visited subclasses … public void accept(Animal animal){ animal.visit(this); }
13
Implementation of Visitor subclasses public class Mouse extends Animal{ public void eats(Animal animal){ animal.accept(this); } public void visit(Cat cat){ s.o.p(“No”); } public void visit(Mouse mouse){ s.o.p(“No”); } … Implementation of Visited subclasses … public void accept(Animal animal){ animal.visit(this); }
14
Putting it all together Animal * +eats (animal: Animal) Visited +accept (v : Visitor) Visitor +visit (cat : Cat) +visit (mouse : Mouse) animal.accept(this) Cat +accept(animal: Animal) +visit (cat : Cat) +visit (mouse : Mouse) Mouse +accept(animal: Animal) +visit (cat : Cat) +visit (mouse : Mouse) animal.visit(this)
15
Different types of Visitor and Visited Employees and Reports example
16
Format print(e :Employee)+ Block visit(r: Regular)+ visit(m: Manager)+ visit(s: Specialist)+ Csv visit(r : Regular)+ visit(m :Manager)+ visit(s :Specialist)+ Employee accept(v: Visitor) Visitor visit(r : Regular) visit(m :Manager) visit(s :Specialist) v.visit(this) e.accept(this) Regular accept(v: Visitor) + Manager accept(v: Visitor)+ Specialist accept(v: Visitor)+ Visited accept(v: Visitor)
17
The Visitor Pattern Advantages Simulating multiple-level dispatch. Type-based treatment without instanceof. Extending the functionality of a class hierarchy by an external class.
18
The Visitor Pattern Shortcomings Tedious to code. Adding a Visited class requires modifying existing Visitor classes. Dispatching multiple arguments becomes inconvenient.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.