COMP2511 Week 10
Agenda
- Admin Stuff
- myExperience
- Visitor Pattern
- Kahoot
Admin Stuff
- Final lab to get all your labs marked off
- Except for Lab 10 which is auto marked
- Assignment 2 interviews continue
- Will need to demo map generation if you have done it.
- Will need to go over pairs who I forgot to demo again
- If 85% of the cohort fills out myExperience, every single student in the course will receive 1 bonus course mark.
myExperience
(10 mins)
Visitor Pattern
Visitor Pattern
Problem: How do I add extra functionalities to subclasses without violating open/closed principle.

Visitor Pattern
Solution: Visitor Pattern
What is the visitor pattern?
- The visitor is a behavioural design pattern that lets you separate algorithms from the objects on which they operate.
- You can add extra functionality to a class without modifying the original (abides by open-closed principle)
- One class/interface (visitor) defines a computation/operation and another (visitable) is responsible for providing data access
Visitor Pattern

Code Demo
In this scenario we have Computers
, Keyboards
and Mouses
which all are of type ComputerComponent
. We want to be able to 'visit' different types of Computer components by logging the following messages:
Looking at computer Corelli with memory 500 GB.
Looking at keyboard Mechanical keyboard which has 36 keys.
Looking at mouse Bluetooth mouse.
In particular though, anyone which is visiting a Computer
must be validated prior to being able to visit.
Extend/modify the starter code to use the Visitor Pattern to allow different computer components to be visited.
Kahoot
Useful Links
Revision
COMP2511 22T3 Week 10
By Jayden Leung
COMP2511 22T3 Week 10
- 92