1/14
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What is the Visitor pattern?
A pattern that lets you define new operations on an object structure without changing the element classes.
What problem does Visitor solve in the slides?
We want to add new reports for employee objects without putting reporting logic inside the employee classes.
Why would putting report code into Employee classes be bad?
It would violate Single Responsibility Principle and make later changes harder.
What is the definition of Visitor?
Represent an operation to be performed on the elements of an object structure; it lets you define a new operation without changing the element classes.
When is Visitor especially useful?
When element classes rarely change, but new operations need to be added often.
What is the abstract element in the slides’ example?
Employee.
What are the concrete elements in the example?
HourlyEmployee and SalariedEmployee.
What is the abstract visitor in the example?
EmployeeVisitor.
What are the concrete visitors in the example?
HourlyPayReport and AllPayReport.
What method do elements implement in Visitor?
accept(visitor).
What does accept() usually do?
It calls visitor->visit(this).
What methods does EmployeeVisitor define?
visit(HourlyEmployee) and visit(SalariedEmployee).
Why not just call visit() directly on an Employee*?
Because visit() needs the concrete subclass type, not just the abstract parent type.
What is one advantage of Visitor?
Adding new operations is easy.
What is one disadvantage of Visitor?
Adding new concrete element classes is hard.