180. Reinterpreting the Visitor pattern via sealed classes and type pattern matching for switch
The Visitor pattern is part of the Gang of Four (GoF) design patterns and its goal is to define a new operation on certain classes without the need to modify those classes. You can find many excellent resources on this topic on the Internet, so for the classical implementation, we will provide here only the class diagram of our example, while the code is available on GitHub:
Figure 8.7: Visitor pattern class diagram (use case)
In a nutshell, we have a bunch of classes (Capacitor
, Transistor
, Resistor
, and ElectricCircuit
) that are used to create electrical circuits. Our operation is shaped in XmlExportVisitor
(an implementation of ElectricComponentVisitor
) and consists of printing an XML document containing the electrical circuit specifications and parameters.
Before continuing, consider getting familiar with the traditional implementation and output of this example available...