JSFiddle - React, Tailwind, and code Playground
by Vlad Rasenko
JavaScript
class Viking {
constructor(moveStrategy) {
this.position = 0
this.moveBehavior = moveStrategy
}
move() {
this.moveBehavior.move(this)
}
}
class Fly {
move(unit) {
unit.position += 10
}
}
class Walk {
move(unit) {
unit.position += 1
}
}
const flyBehavior = new Fly()
const walkBehavior = new Walk()
const flyViking = new Viking(flyBehavior)
const walkViking = new Viking(walkBehavior)
flyViking.move()