From the course: Java Design Patterns: Behavioral Part 2

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Solution: The Visitor pattern

Solution: The Visitor pattern - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 2

Start my 1-month free trial

Solution: The Visitor pattern

(upbeat music) - Let's have a look at my solution to this exercise. So I've created an interface called visitor, which has three visit methods in it. Each one sets a different type of employee as an argument. Then I have a concrete implementation called salary adjustment visitor. I'm sure yours will be called something slightly different. In here, I'm setting the manager salary to 65,000, and the sales person's salary to 57,000. And for the staff list, I'm printing out a message that says that salary adjustments have been applied to all staff. And again, I'm sure your message will be slightly different. Then in the employee interface, I've added a new method called accepts, which takes a visitor as an argument. And in the manager and salesperson classes, I'm implementing this method and I'm doing visitor.visit and passing in this. In the staff list class, the implementation is slightly different. I'm calling the accepts…

Contents