Chapter 10. Behavioral Patterns: Visitor, Interpreter, and Memento

In this chapter, we'll consider the final three core design patterns. The Visitor and Interpreter patterns are well-known in compiler-writing circles. However, they also apply wherever operations are defined on structures according to specific rules, and the operations and structures are defined separately. The Memento pattern rounds out the behavioral group by showing how the state of objects can be saved and restored.

Visitor Pattern

Role

The Visitor pattern defines and performs new operations on all the elements of an existing structure, without altering its classes.

Illustration

This illustration takes us into the realms of student record systems. These days, it's typical for courses to be assessed in several ways, which all add up to a final result. At the start of a course, instructors are required to declare the components that will contribute to the assessment. The students' marks are recorded throughout the semester and then totaled up at the end according to the weights that were specified. For example, consider the extract from a spreadsheet containing the marks for a course on Programming Languages in Figure 10-1. We can deduce that the assessment for the course consisted of:

  • Five laboratories (or labs) (2 percent each)

  • A midterm evaluation consisting of a laboratory component (10 percent) and a theory component (15 percent)

  • An assignment that is a take-home laboratory (10 percent)

  • An examination consisting ...

Get C# 3.0 Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.