From harness-claude
Implements the State pattern in JavaScript to encapsulate state-dependent behavior into separate classes, eliminating complex conditionals.
How this skill is triggered — by the user, by Claude, or both
Slash command
/harness-claude:js-state-patternThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
> Allow an object to alter its behavior when its internal state changes
Allow an object to alter its behavior when its internal state changes
context.setState(newState).class TrafficLight {
constructor() {
this.state = new RedState(this);
}
setState(state) {
this.state = state;
}
signal() {
this.state.signal();
}
}
class RedState {
constructor(light) {
this.light = light;
}
signal() {
console.log('Red — stop');
this.light.setState(new GreenState(this.light));
}
}
class GreenState {
constructor(light) {
this.light = light;
}
signal() {
console.log('Green — go');
this.light.setState(new RedState(this.light));
}
}
const light = new TrafficLight();
light.signal(); // Red — stop
light.signal(); // Green — go
The State pattern encapsulates state-dependent behavior into separate classes. The context object delegates to the current state, and states handle transitions. This eliminates complex conditionals and makes adding new states straightforward.
Trade-offs:
When NOT to use:
https://patterns.dev/javascript/state-pattern
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeReplaces conditional logic with state objects that delegate behavior to the current state. Useful for state machines like order lifecycle, connection states, or traffic lights.
Use when an object's behavior changes based on its internal state and the logic for each state is complex — replacing large conditional chains with state objects where each state encapsulates its own behavior.
Generates State pattern for PHP 8.4 including context, state interface, concrete states, factory, entity updates, exceptions, and unit tests for state-dependent object behavior like order workflows.