Start learning free

LESSON 324 OF 343 · DESIGN PATTERNS

State Pattern

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 struct 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 interface State has four methods. An interface lists the methods a type must have. Any struct with those methods can be used as one. 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.

Go has no default methods. So each state writes all three actions. An action that changes nothing returns the state itself, this.

Write three structs with the methods of 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 struct Post:

  • NewPost() 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.

Go has no classes. Each state is a struct with no fields, such as Draft{}. NewPost() makes a new post.

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 22
    Ctrl + Enter to check