0

State pattern example of typescript

Osho 2 years ago updated 2 years ago 0

Hi, there is lack of code in State pattern example of typescript.
https://refactoring.guru/ja/design-patterns/state/typescript/example

This code needs "state.setContext()" in constructor of Context class maybe.
Like this,

class Context {
    /**
     * @type {State} A reference to the current state of the Context.
     */
    private state: State;

    constructor(state: State) {
        this.transitionTo(state);
        this.state.setContext(this); // add setContext() for state
    }
...

Please check this example.

Thank you.