The Shapeshifter's Core
Allow an entity to alter its behavior entirely when its internal magical state changes.
β§ GANG OF FOUR LINEAGE & ARCANUM β§
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Transmutation // Polymorph
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// DART PATTERN //
abstract class DruidState {
void attack();
}
class HumanForm implements DruidState {
@override
void attack() => print('Swings a wooden staff.');
}
class BearForm implements DruidState {
@override
void attack() => print('Mauls with savage claws!');
}
class Druid {
DruidState _state;
Druid(this._state);
void polymorph(DruidState newState) {
_state = newState;
print('Druid shifted forms.');
}
void attack() => _state.attack();
}
void main() {
final druid = Druid(HumanForm());
druid.attack();
druid.polymorph(BearForm());
druid.attack(); // Behavior fundamentally changed
}
Gigantic switch-statements are the mark of a novice transmuter. The State pattern encapsulates form-specific behaviors into distinct classes. As the entityβs state shifts, its underlying class pointer is swapped, polymorphing its behavior seamlessly.
Runic Tags:
#dart
#state
#polymorph
#state-machine