The Visitor: The Diagnostic Probe
Represent an operation to be performed on the elements of an object structure.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Visitor
ARCANUM SCHOOL: Divination // Inspection
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
public interface GNOMEArtifice.ConstructVisitor : Object {
public abstract void visit_turret(TurretNode turret);
public abstract void visit_drone(DroneNode drone);
}
public interface GNOMEArtifice.ConstructElement : Object {
public abstract void accept(ConstructVisitor visitor);
}
public class GNOMEArtifice.TurretNode : Object, ConstructElement {
public int ammo = 100;
public void accept(ConstructVisitor visitor) {
visitor.visit_turret(this);
}
}
public class GNOMEArtifice.DroneNode : Object, ConstructElement {
public int battery = 50;
public void accept(ConstructVisitor visitor) {
visitor.visit_drone(this);
}
}
public class GNOMEArtifice.DiagnosticScanner : Object, ConstructVisitor {
public void visit_turret(TurretNode turret) {
print(@"Turret diagnostics: $(turret.ammo) rounds remaining.\n");
}
public void visit_drone(DroneNode drone) {
print(@"Drone diagnostics: $(drone.battery)% battery charge.\n");
}
}
When managing a sprawling defensive line of Constructs, adding new operational logic—like a diagnostic scan—directly into the classes bloats them and risks destabilizing the weapon systems. The Visitor pattern acts as a ghost in the machine. You unleash a DiagnosticScanner probe. Each Construct merely accept()s the probe, granting it access. The probe performs its arcane calculations externally, allowing the GNOME Artifice to append endless new operations without ever rewriting the core hardware definitions.
Runic Tags:
#Vala
#GObject
#Behavioral
#Visitor