The Spellbook Interchange
Define a family of interchangeable algorithms, encapsulating each one dynamically.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Strategy
ARCANUM SCHOOL: Evocation // Tactical Casting
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// DART PATTERN //
abstract class DamageStrategy {
int calculateDamage(int baseDamage);
}
class CriticalStrike implements DamageStrategy {
@override
int calculateDamage(int baseDamage) => baseDamage * 2;
}
class PoisonVampirism implements DamageStrategy {
@override
int calculateDamage(int baseDamage) {
print('Siphoning life force...');
return baseDamage + 5;
}
}
class Mage {
DamageStrategy strategy;
Mage(this.strategy);
void castSpell() {
final damage = strategy.calculateDamage(10);
print('Dealt $damage damage.');
}
}
void main() {
final mage = Mage(CriticalStrike());
mage.castSpell();
mage.strategy = PoisonVampirism(); // Hot-swap the algorithmic strategy
mage.castSpell();
}
A master tactician never brings only one spell to a duel. The Strategy pattern allows you to define a family of algorithms, put them in interchangeable spellbooks, and hot-swap them at runtime. The execution logic belongs to the strategy, keeping the caster clean.
Runic Tags:
#dart
#strategy
#algorithm
#tactical