Strategy?
by Artem
JavaScript
'use strict';
class Greeter {
constructor(strategy) {
this.strategy = strategy;
}
greet() {
this.strategy.execute();
}
}
class Strategy {
constructor() {
throw new Error('Can\'t create an instance of abstract class');
}
execute() {
throw new Error('Can\'t call abstract method');
}
}
class GreetingStrategy extends Strategy {
constructor() {
// ???
}
execute() {
console.log('Hello');
}
}
class AnotherStrategy extends Strategy {
constructor() {
}
execute() {
console.log('Hi');
}
}
console.log(new GreetingStrategy());
(new Greeter(new GreetingStrategy())).greet();
//(new Greeter(new AnotherStrategy())).greet();