TypeScript Memento Pattern Example

In this article, we will learn how to use and implement the Memento Pattern in TypeScript with an example.
Memento is a behavioral design pattern that lets you save and restore the previous state of an object without revealing the details of its implementation.

TypeScript Memento Pattern Example

The below diagram shows the generic structure of the Memento Pattern:
Let's refer to the above structure to create an example to demonstrates the usage of the Memento Pattern.
Let's create memento.ts file and add the following code to it:
export class State {
    private str: string;

    constructor(str: string) {
        this.str = str;
    }

    get Str() : string {
        return this.str;
    }

    set Str(str: string) {
        this.str = str;
    }
}

export class Originator {
    private state: State;

    constructor(state: State) {
        this.state = state;
    }

    get State(): State {
        return this.state;
    }

    set State(state: State) {
        console.log("State :: ", state);
        this.state = state;
    }

    public createMemento(): Memento {
        console.log("creates a memento with a given state!");
        return new Memento(this.state);
    }

    public setMemento(memento: Memento) {
        console.log("sets the state back");
        this.State = memento.State;
    }
}

export class Memento {
    private state: State;

    constructor (state: State) {
        this.state = state;
    }

    get State(): State {
        console.log("get memento's state");
        return this.state;
    }
}

export class CareTaker {
    private memento: Memento;

    get Memento(): Memento {
        return this.memento;
    }

    set Memento(memento: Memento) {
        this.memento = memento;
    }
}

Usage

Let's create demo.ts file and add the following code to it:
import { State, Originator, CareTaker } from "./memento";

export function show() : void {
 var state: State = new State("... State "),
  originator: Originator = new Originator(state),
  careTaker: CareTaker = new CareTaker();

 careTaker.Memento = originator.createMemento();
 originator.State = new State("something else...");

 originator.setMemento(careTaker.Memento);
}
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\memento> tsc --target ES5 .\demo.ts
design_patterns_in_typescript\memento> node .\demo.js
creates a memento with a given state!
State ::  State { str: 'something else...' }
sets the state back
get memento's state
State ::  State { str: '... State ' }

All TypeScript Design Patterns

1. Creational Design Patterns

Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
  1. TypeScript Singleton Pattern Example
  2. TypeScript Factory Design Pattern with Example
  3. TypeScript Abstract Factory Pattern Example
  4. TypeScript Builder Pattern Example
  5. 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.
  1. TypeScript Bridge Pattern Example
  2. TypeScript Adapter Pattern Example
  3. TypeScript Decorator Pattern Example
  4. TypeScript Composite Pattern Example
  5. TypeScript Flyweight Design Pattern Example
  6. TypeScript Facade Pattern Example
  7. TypeScript Proxy Pattern Example

3. Behavioral Design Patterns

Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
  1. TypeScript Command Pattern Example
  2. TypeScript Chain of Responsibility Pattern Example
  3. TypeScript Visitor Pattern Example
  4. TypeScript Template Method Pattern Example
  5. TypeScript Strategy Pattern Example
  6. TypeScript State Pattern Example
  7. TypeScript Observer Pattern Example
  8. TypeScript Memento Pattern Example
  9. TypeScript Mediator Pattern Example
  10. TypeScript Iterator Pattern Example
  11. TypeScript Interpreter Design Pattern Example

Comments