Problem: How do I add extra functionalities to subclasses without violating open/closed principle.
Solution: Visitor Pattern
What is the visitor pattern?
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.