Conceptual Example
This example illustrates the structure of the Visitor design pattern and focuses on the following questions:
- What classes does it consist of?
- What roles do these classes play?
- In what way the elements of the pattern are related?
After learning about the pattern’s structure it’ll be easier for you to grasp the following example, based on a real-world PHP use case.
index.php: Conceptual example
Output.txt: Execution result
Real World Example
In this example, the Visitor pattern helps to introduce a reporting feature into an existing class hierarchy: Company > Department > Employee
Once the Visitor infrastructure is added to the app, you can easily add other similar behaviors to the app, without changing the existing classes.
index.php: Real world example
Output.txt: Execution result