Visitor or pattern matching? Ask which list grows
transcript
Kinds of item, or operations on them? The answer picks your design.
Methods on items. A new kind is one class. A new operation edits every class. Now a visitor, or a checked match. A new operation is one class. A new kind edits every visitor or match.
A gift card arrives. One new class. One new visit method in the interface. And that method in all five visitors that already exist.
Many languages can do this without a visitor. Plain data, and one function per operation. In C plus plus, a missing kind won't compile. Java 21 agrees. No accept, and no second class hierarchy. Often that's lighter.
Visitor still wins when the element classes aren't yours, but already take a visitor. Or when operations should be objects. Or when the language has no checked match.
Kinds growing? Put methods on the items. Operations growing? Visitor, or a checked match. Ask before you build.
Full breakdown in the lecture.