Visitor Pattern

The visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existent object structures without modifying the structures. It is one way to follow the open/closed principle.

In essence, the visitor allows adding new virtual functions to a family of classes, without modifying the classes. Instead, a visitor class is created that implements all of the appropriate specializations of the virtual function. The visitor takes the instance reference as input, and implements the goal through double dispatch.

Wikipedia: Visitor Pattern


View on GitHub

abstract class BodyPart {
  String name;
  void accept(BodyPartVisitor visitor) => visitor.visit(this);

abstract class BodyPartVisitor {
  void visit(BodyPart bodyPart);

class Face extends BodyPart {
  String name = "Face";

class Finger extends BodyPart {
  String name = "Finger";

class Foot extends BodyPart {
  String name = "Foot";

class Body implements BodyPart {
  String name = "Body";
  List<BodyPart> bodyParts;

  Body() {
    bodyParts = List.from([
      Face(), Finger("Index Finger"), Finger("Thumb"),
      Foot("Left Foot"), Foot("Right Foot"), Foot("Third Foot?")

  void accept(BodyPartVisitor visitor) {
    for (var bodyPart in bodyParts) {

class BodyPartStitchVisitor implements BodyPartVisitor {
  void visit(BodyPart bodyPart) {
    if (bodyPart is Body) { print("Reanimating my ${}."); }
    if (bodyPart is Face) { print("Stitching together my ${}."); }
    if (bodyPart is Finger) { print("Stitching together my ${}."); }
    if (bodyPart is Foot) { print("Gluing on my ${}."); }

class BodyPartProdVisitor implements BodyPartVisitor {
void visit(BodyPart bodyPart) {
    if (bodyPart is Body) { print("Poking my ${} with an electrode."); }
    if (bodyPart is Face) { print("Prodding ${}."); }
    if (bodyPart is Finger) { print("Pricking ${}."); }
    if (bodyPart is Foot) { print("Tickling ${}."); }

void main() {
  var body = Body();

    Stitching together my Face.
    Stitching together my Index Finger.
    Stitching together my Thumb.
    Gluing on my Left Foot.
    Gluing on my Right Foot.
    Gluing on my Third Foot?.
    Reanimating my Body.
    Prodding Face.
    Pricking Index Finger.
    Pricking Thumb.
    Tickling Left Foot.
    Tickling Right Foot.
    Tickling Third Foot?.
    Poking my Body with an electrode.