Visitor Design Pattern
Structural: Visitor Pattern
Can you explain the visitor design pattern?
View Answer:
Interview Response: Visitor is a behavioral design pattern that lets you detach algorithms from the objects on which they act. The Visitor pattern adds new methods to a group of objects without affecting them, and the new logic gets housed in a distinct entity known as the Visitor.
Code Example:


The objects participating in this pattern are:
ObjectStructure -- example code: employees array
- maintains a collection of elements that can get iterated over
Elements -- example code: Employee objects
- defines an accept method that accepts visitor objects
- in the accept method, the Visitor's visit method gets invoked with 'this' as a parameter
Visitor -- example code: ExtraSalary, ExtraVacation
- implements a visit method. The element getting visited is the argument when the element's changes get made.
let Employee = function (name, salary, vacation) {
let self = this;
this.accept = function (visitor) {
visitor.visit(self);
};
this.getName = function () {
return name;
};
this.getSalary = function () {
return salary;
};
this.setSalary = function (sal) {
salary = sal;
};
this.getVacation = function () {
return vacation;
};
this.setVacation = function (vac) {
vacation = vac;
};
};
let ExtraSalary = function () {
this.visit = function (emp) {
emp.setSalary(emp.getSalary() * 1.1);
};
};
let ExtraVacation = function () {
this.visit = function (emp) {
emp.setVacation(emp.getVacation() + 2);
};
};
function run() {
let employees = [
new Employee('John', 10000, 10),
new Employee('Mary', 20000, 21),
new Employee('Boss', 250000, 51),
];
let visitorSalary = new ExtraSalary();
let visitorVacation = new ExtraVacation();
for (let i = 0, len = employees.length; i < len; i++) {
let emp = employees[i];
emp.accept(visitorSalary);
emp.accept(visitorVacation);
console.log(
emp.getName() +
': $' +
emp.getSalary() +
' and ' +
emp.getVacation() +
' vacation days'
);
}
}
run();
/*
OUTPUT:
John: $11000 and 12 vacation days
Mary: $22000 and 23 vacation days
Boss: $275000 and 53 vacation days
*/
To which pattern family does the Visitor pattern belong?
View Answer:
Interview Response: The Visitor pattern is part of the Behavioral design pattern set.
When should you utilize the JavaScript Visitor Pattern?
View Answer:
Interview Response: We can use the visitor pattern when:
- Similar procedures must get done on various data structure objects.
- Specific operations must get carried out on multiple items in the data structure.
- You wish to make libraries or frameworks more extensible.
What are some of the benefits of using the Visitor pattern?
View Answer:
Interview Response: Benefits of the Visitor Pattern
- The principle of open/closed. You may add new behavior that works with objects of various classes without modifying the classes themselves.
- Single Responsibility Principle. You can move multiple versions of the same behavior into the same class.
- While working with various objects, a visitor object might get helpful information. This information is helpful if you wish to traverse a complicated object structure, such as an object tree, and apply the Visitor to each item in the structure.
What are some of the Visitor pattern's drawbacks?
View Answer:
Interview Response: Drawbacks of the Visitor Pattern.
- Every time a class is added or withdrawn from the element hierarchy, you must notify all visitors.
- Visitors may not have access to the private fields and methods of the components they get expected to operate.