In this article, we will learn how to use and implement the Strategy Pattern in TypeScript with an example.
Strategy pattern defines a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.
TypeScript Strategy Pattern Example
The below diagram shows the generic structure of the Strategy Pattern:
Let's refer to the above structure to create an example to demonstrates the usage of the Strategy Pattern.
Let's create strategy.ts file and add the following code to it:
export interface Strategy {
execute(): void;
}
export class ConcreteStrategy1 implements Strategy {
public execute(): void {
console.log("`execute` method of ConcreteStrategy1 is being called");
}
}
export class ConcreteStrategy2 implements Strategy {
public execute(): void {
console.log("`execute` method of ConcreteStrategy2 is being called");
}
}
export class ConcreteStrategy3 implements Strategy {
public execute(): void {
console.log("`execute` method of ConcreteStrategy3 is being called");
}
}
export class Context {
private strategy: Strategy;
constructor(strategy: Strategy) {
this.strategy = strategy;
}
public executeStrategy(): void {
this.strategy.execute();
}
}
Usage
Let's create demo.ts file and add the following code to it:
import { ConcreteStrategy1, ConcreteStrategy2, ConcreteStrategy3, Context } from "./strategy";
export function show() : void {
var context: Context = new Context(new ConcreteStrategy1());
context.executeStrategy();
context = new Context(new ConcreteStrategy2());
context.executeStrategy();
context = new Context(new ConcreteStrategy3());
context.executeStrategy();
}
show();
Run:
- Compile the above code using the TypeScript compiler.
- Above code is compiled to plan JavaScript code
- Run Javascript code using node
design_patterns_in_typescript\strategy> tsc --target ES5 .\demo.ts
design_patterns_in_typescript\strategy> node .\demo.js
`execute` method of ConcreteStrategy1 is being called
`execute` method of ConcreteStrategy2 is being called
`execute` method of ConcreteStrategy3 is being called
All TypeScript Design Patterns
1. Creational Design Patterns
Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
- TypeScript Singleton Pattern Example
- TypeScript Factory Design Pattern with Example
- TypeScript Abstract Factory Pattern Example
- TypeScript Builder Pattern Example
- TypeScript Prototype Pattern Example
2. Structural Design Patterns
Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.
- TypeScript Bridge Pattern Example
- TypeScript Adapter Pattern Example
- TypeScript Decorator Pattern Example
- TypeScript Composite Pattern Example
- TypeScript Flyweight Design Pattern Example
- TypeScript Facade Pattern Example
- TypeScript Proxy Pattern Example
3. Behavioral Design Patterns
Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
- TypeScript Command Pattern Example
- TypeScript Chain of Responsibility Pattern Example
- TypeScript Visitor Pattern Example
- TypeScript Template Method Pattern Example
- TypeScript Strategy Pattern Example
- TypeScript State Pattern Example
- TypeScript Observer Pattern Example
- TypeScript Memento Pattern Example
- TypeScript Mediator Pattern Example
- TypeScript Iterator Pattern Example
- TypeScript Interpreter Design Pattern Example