From harness-claude
Adds new operations to object structures without modifying them, using the Visitor pattern with JavaScript examples for AST, file trees, and DOM.
How this skill is triggered — by the user, by Claude, or both
Slash command
/harness-claude:js-visitor-patternThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
> Add new operations to object structures without modifying the objects
Add new operations to object structures without modifying the objects
accept(visitor) method on each element class in the object structure.accept calls the visitor's corresponding method, passing this (double dispatch).visitFile, visitFolder).class File {
constructor(name, size) {
this.name = name;
this.size = size;
}
accept(visitor) {
return visitor.visitFile(this);
}
}
class Folder {
constructor(name, children) {
this.name = name;
this.children = children;
}
accept(visitor) {
return visitor.visitFolder(this);
}
}
const sizeCalculator = {
visitFile(file) {
return file.size;
},
visitFolder(folder) {
return folder.children.reduce((sum, child) => sum + child.accept(this), 0);
},
};
The Visitor pattern achieves the open/closed principle by separating operations from the object structure. New operations (visitors) can be added without modifying existing element classes. This is heavily used in compilers and AST processors (e.g., Babel plugins are visitors).
Trade-offs:
When NOT to use:
map/reduce over an array is clearerhttps://patterns.dev/javascript/visitor-pattern
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeImplements the Visitor pattern for adding operations to object structures via double dispatch. Includes TypeScript examples for AST evaluation, printing, and counting.
Adds new operations to stable object structures without modifying element classes, using the Visitor pattern. Useful for AST analysis, code quality tools, and compiler passes.
Generates Visitor pattern for PHP 8.4 to perform operations on object structures without modifying element classes. Includes visitor interface, concrete visitors, visitable elements, and unit tests.