Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Module 4] Visitor design pattern - Calculator #38

Open
xgillard opened this issue Dec 2, 2020 · 1 comment
Open

[Module 4] Visitor design pattern - Calculator #38

xgillard opened this issue Dec 2, 2020 · 1 comment

Comments

@xgillard
Copy link
Collaborator

xgillard commented Dec 2, 2020

**This issue relates to #35 ** (should probably be a subtask of it).

This exercise is a wrong use case of the visitor pattern. The appropriate pattern to use here is the Interpreter pattern.
While it is true that one can implement a grammar-like interpretation with a visitor, this is pretty artificial and forces to break encapsulation. (Encapsulation breakage is the major inconvenient of Visitor pattern and built in it).

An exercise about the visitor pattern should start by recalling what double dispatch is and provide an example that requires it.

@xgillard
Copy link
Collaborator Author

xgillard commented Dec 2, 2020

Here are a few cool examples that would be better fits for visitor:

  1. University management system.
    One can expect to have the following objects defined:
  • Faculties
  • Departments
  • Technical/Support personnel
  • Teaching staff
  • Researchers
  • Students
  • Building
  • Classes
  • Courses
  • Computers
  • .. whatever machinery you like and more

There are many tasks which one might want to define on those:

  • Define the budget of the Univ. (positive income for students, negative for staff and resources)
  • Prepare candidate time tables for examinations (a class that can accomodate the students of a particular course).
  • Prepare the payroll
  • ...
  1. An other maybe more fun example of use for Visitor
    When implementing a videogame, you're likely to use an ECS (entity component system). Depending on the moment of the day and the material of each entity in the system, the rendering engine might need to paint the same objects in various colors at different moment of time.

A convenient approach to that would be to let the 'renderer' system pick the painting algo. (whichone implements visitor) based on the time of the day and wheather conditions (those are entities that can be queries from the ECS, but we'll assume they're just given parameters). The chosen painting sub-service would then visit all the visible physical entities of the scene (visited entity decides whether it is visible or not) and paint them according to the appropriate color scheme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant