Start learning free

LESSON 324 OF 343 · DESIGN PATTERNS

State Pattern in JavaScript

Practice Behavioral

Some objects act differently depending on the state they are in. A blog post in review can be approved, but a draft can't. An if on the state in every method soon gets hard to follow.

The State pattern makes each state a class of its own. Each state handles every action in its own way and returns the state that comes next. The object keeps its current state and hands each action to it.

Here you write the review workflow of a blog post. A post is a draft, in review or published.

The base class State has four methods. It plays the part of an interface, a list of the methods every state must have. Each state class extends it. Its name() returns the state's name. Its submit(), approve() and reject() are the three actions. Each of them returns an object: the state that comes next.

In State, the three actions have a default body. It returns this, the state itself, so nothing changes. A state writes its own version only of the actions that change it.

Write three classes built on State:

  • Draft is named "draft". Its submit() sends the post to review. Approving or rejecting a draft changes nothing.
  • Review is named "review". Its approve() publishes the post, and its reject() sends it back to draft. Submitting it again changes nothing.
  • Published is named "published". Its reject() takes the post down, back to draft. Submitting or approving it changes nothing.

Then write the class Post:

  • new Post() makes a new post. It starts as a draft.
  • submit(), approve() and reject() hand the action to the current state. The post keeps the state that comes back.
  • state() returns the current state.

The tests call only these methods of a post. After each action they write post.state().name(). So the post must keep a state object, not a word.

Each example is a list of commands, one per line:

  • new makes a new post and writes its number. The posts are numbered 1, 2, 3 and so on.
  • submit I, approve I and reject I act on post I. Then they write the name of its state.

 

Example 1:

Input:
1. new
2. submit 1
3. approve 1
4. reject 1
5. submit 1
6. reject 1
7. submit 1
8. approve 1
Output:
1
review
published
draft
review
draft
review
published
Explanation:
Command 1 makes post 1, a draft.
Commands 2 and 3 submit it and approve it, so it is published.
Command 4 takes it down, back to draft.
Command 6 rejects it in review, so it is a draft again.
Commands 7 and 8 publish it once more.

Example 2:

Input:
1. new
2. new
3. approve 1
4. reject 1
5. submit 2
6. submit 2
7. approve 2
8. submit 2
9. approve 2
10. submit 1
Output:
1
2
draft
draft
review
review
published
published
published
review
Explanation:
A draft can't be approved or rejected, so post 1 stays a draft.
Submitting post 2 twice leaves it in review.
Once it is published, submit and approve change nothing.
Each post has its own state. So the last command moves only post 1.

 

Constraints:

  • Each example starts with no posts.
  • I is the number of a post made earlier in the same example.
  • There are at most 100,000 commands in all.

Note:

Each call should run in O(1) time. An action asks the current state once and keeps what it returns.

STEP 1 OF 23
    Ctrl + Enter to check

    State Pattern in JavaScript

    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free