From the course: Java Design Patterns: Behavioral Part 2
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Implementing the full Visitor pattern - Java Tutorial
From the course: Java Design Patterns: Behavioral Part 2
Implementing the full Visitor pattern
- [Instructor] Now I've created the visitor objects. I need to finish off the visitor pattern by making sure each elements can accept the visitor. So first, I'm going to go to the groceries interface. And I'm going to define a new methods which are called accepts. So I'm going to do void accept. And this method is going to take a visitor object as an argument. Now I need to go ahead and implement this method in the concrete implementations of groceries. So I'm going to go to the bread class and under the get price method, I'm going to do public void accept and pass in a visitor object. Then in this method, I'm going to do visitor.visit. So there's a different visit method for each different type of grocery. So I want to pass in bread. So as this is the bread class, I'm going to pass in this. Now, I'm going to copy this method. And I'm going to go to the milk class and I'm going to paste this method in here. Finally…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.