Complete the code to declare the Strategy interface.
interface [1] {
execute(): void;
}The Strategy pattern requires a common interface named Strategy that all concrete strategies implement.
Complete the code to define a concrete strategy class implementing the Strategy interface.
class ConcreteStrategyA implements [1] { execute(): void { console.log('Strategy A executed'); } }
The concrete strategy class must implement the Strategy interface to provide its own algorithm.
Fix the error in the Context class constructor to accept a strategy.
class Context { strategy: [1]; constructor(strategy: [1]) { this.strategy = strategy; } executeStrategy(): void { this.strategy.execute(); } }
The Context class should accept an object of type Strategy to delegate the execution to the chosen algorithm.
Fill both blanks to change the strategy at runtime and execute it.
const context = new Context(new ConcreteStrategyA()); context.executeStrategy(); context.[1] = new ConcreteStrategyB(); context.[2]();
The Context's strategy property can be reassigned to change the algorithm, and executeStrategy() runs the current strategy.
Fill all three blanks to implement a new concrete strategy and use it in the context.
class [1] implements Strategy { execute(): void { console.log('Strategy C executed'); } } const context = new Context(new [2]()); context.[3]();
The new strategy class is named ConcreteStrategyC. The context is created with this strategy and calls executeStrategy() to run it.
