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 trait State has four methods. A trait lists the methods a type promises to have. 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.
Each state writes all three actions. An action that changes nothing returns a new box of the same state, such as Box::new(Draft). A state has no fields, so that is the same state.
Write three structs that implement State:
Draftis named"draft". Itssubmit()sends the post to review. Approving or rejecting a draft changes nothing.Reviewis named"review". Itsapprove()publishes the post, and itsreject()sends it back to draft. Submitting it again changes nothing.Publishedis named"published". Itsreject()takes the post down, back to draft. Submitting or approving it changes nothing.
Then write the struct Post:
Post::new()makes a new post. It starts as a draft.submit(),approve()andreject()hand the action to the current state. The post keeps the state that comes back.state()returns the current state.
Rust has no classes. Each state is a struct with no fields, such as Draft. A post keeps its state as a Box<dyn State>. Here dyn State means any struct that implements 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:
newmakes a new post and writes its number. The posts are numbered 1, 2, 3 and so on.submit I,approve Iandreject Iact on postI. 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.
Iis 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.
Free plan: 30 tutor messages. PRO is unlimited.
