Expand description

The Visitor design pattern example module

The Visitor pattern is used to add functionality to a list of otherwise unchanging element objects by passing a visitor object to each element object. Each element object calls the visitor object, passing itself as an argument. The visitor object then does something based on the type of the element.

In this exercise, a collection of shop objects is initialized then an order visitor is created to retrieve an item from one of the shop objects. Along the way, shops that don’t have the necessary ingredients use another order visitor to order ingredients from other shops. This approach assumes no two shops sell the same thing.

Accessed through the visitor_exercise() function.

Modules

  • Contains the OrderVisitor struct that represents the visitor going around and visiting the shops of the village.
  • Contains the VisitorShop struct, which represents each shop in the village that can be visited.
  • Contains the Village struct that holds all the shops that can be visited.

Functions