🟨 JAVASCRIPT PATTERN DIFFICULTY: Adept Behavioral

The Shapeshifter State

Allow an entity to alter its behavior when its internal state changes, appearing to change its class.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Transmutation // Metamorphosis
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// JAVASCRIPT IDIOMATIC PATTERN //
class Form { attack() {} }

class HumanForm extends Form {
  attack() { console.log("Punches with fist."); }
}
class WolfForm extends Form {
  attack() { console.log("Bites with fangs!"); }
}

class Lycanthrope {
  constructor() { this.state = new HumanForm(); }
  transform(newState) { this.state = newState; }
  attack() { this.state.attack(); }
}

const werewolf = new Lycanthrope();
werewolf.attack(); // Human

werewolf.transform(new WolfForm());
werewolf.attack(); // Wolf

The Shapeshifter State

A shapeshifter does not need to learn how to bite; the knowledge is inherent to the wolf form. The State pattern allows dynamic swapping of an object’s behavior at runtime.

Runic Tags: #state #behavior #shapeshifting