In this article, we will learn how to use and implement the Interpreter Pattern in TypeScript with an example.
Interpreter pattern provides a way to evaluate language grammar or expression. This type of pattern comes under behavioral patterns. This pattern involves implementing an expression interface which tells to interpret a particular context.
TypeScript Interpreter Design Pattern Example
Let's create interpreter.ts file and add the following code to it:
export class Context {
}
export interface AbstractExpression {
interpret(context: Context): void;
}
export class TerminalExpression implements AbstractExpression {
public interpret(context: Context): void {
console.log("`interpret` method of TerminalExpression is being called!");
}
}
export class NonterminalExpression implements AbstractExpression {
public interpret(context: Context): void {
console.log("`interpret` method of NonterminalExpression is being called!");
}
}
Usage
Let's create demo.ts file and add the following code to it:
import { NonterminalExpression, TerminalExpression, Context } from "./interpreter";
export function show() : void {
var context: Context = new Context(),
list = [],
i = 0,
max;
list.push(new NonterminalExpression());
list.push(new NonterminalExpression());
list.push(new NonterminalExpression());
list.push(new TerminalExpression());
list.push(new NonterminalExpression());
list.push(new NonterminalExpression());
list.push(new TerminalExpression());
list.push(new TerminalExpression());
for (i = 0, max = list.length; i < max; i += 1) {
list[i].interpret(context);
}
}
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\interpreter> tsc --target ES5 .\demo.ts
design_patterns_in_typescript\interpreter> node .\demo.js
`interpret` method of NonterminalExpression is being called!
`interpret` method of NonterminalExpression is being called!
`interpret` method of NonterminalExpression is being called!
`interpret` method of TerminalExpression is being called!
`interpret` method of NonterminalExpression is being called!
`interpret` method of NonterminalExpression is being called!
`interpret` method of TerminalExpression is being called!
`interpret` method of TerminalExpression 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
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course