mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-06-16 05:24:17 -06:00
242 lines
9.3 KiB
Markdown
242 lines
9.3 KiB
Markdown
# Implementing an Object-Oriented Design Pattern
|
|
The *state pattern* is an object-oriented design pattern.
|
|
|
|
The crux of the pattern is that we define a set of states a value can have internally.
|
|
|
|
The states are represented by a set of *state objects* and the value's behavior changes based on its state.
|
|
|
|
We are going to work through an example of a blog post struct that has aa filed to hold its state, this will be a state object form the set "draft", "review", or "published".
|
|
|
|
The sate objects share functionality. In Rust we use structs and traits rather than objects and inheritance.
|
|
|
|
Each state object is responsible for its own behavior and for governing when it should change into another state.
|
|
|
|
The value that holds a state object knows nothing about the different behavior of the states or when to transition between states.
|
|
|
|
|
|
The advantage of using this is that when the business requirements of the program change, we won't need to change the code of the value holding the state or the code that uses the value.
|
|
|
|
We will only need to update the code inside one of the state objects to change its rules or perhaps add more state objects.
|
|
|
|
We are going to implement the state pattern in a more tradition object-oriented way.
|
|
|
|
Next we will use an approach that is a bit more natural in Rust.
|
|
|
|
Lets start with incrementally implementing a blog post workflow using the state pattern.
|
|
|
|
The final functionality will look like this:
|
|
1. A blog post starts as an empty draft
|
|
2. When the draft is complete, a review of the post is requested
|
|
3. When the post is approved, it will be published
|
|
4. Only published blog posts return content to print, so unapproved posts can't be accidentally be published
|
|
Any other changes attempted on a post should have no effect.
|
|
|
|
An example of this is if we try to approve a draft blog post before we have requested a review, the post should remain an unpublished draft.
|
|
|
|
In this example, it shows the workflow in code form.
|
|
|
|
This is an example usage of the API, we will implement in a library crate named `blog`.
|
|
|
|
This will not compile yet because we haven't implemented the `blog` crate.
|
|
|
|
```rust
|
|
use blog::Post;
|
|
|
|
fn main() {
|
|
let mut post = Post::new();
|
|
|
|
post.add_text("I ate a salad for lunch today");
|
|
assert_eq!("", post.content());
|
|
|
|
post.request_review();
|
|
assert_eq!("", post.content());
|
|
|
|
post.approve();
|
|
assert_eq!("I ate a salad for lunch today", post.content());
|
|
}
|
|
```
|
|
We want to allow the user to create a new draft blog post with `Post::new`.
|
|
|
|
We also want to allow text to be added to be blog post.
|
|
|
|
If we try to get the post's content immediately, before approval, we shouldn't get any text because the post is still a draft.
|
|
|
|
We added `assert_eq!` for demonstration purposes.
|
|
|
|
An excellent unit test for this would be to assert that a draft post returns an empty string from the `content` method, but we are not going to write tests for this example.
|
|
|
|
Next, we want to enable a request for a review of the post and we want `content` to return an empty string while waiting for the review.
|
|
|
|
Then when the post receives approval, it should get published, meaning the text of the post will be returned when `content` is called.
|
|
|
|
Note that the only type we are interacting with from the crate s the Post type.
|
|
|
|
This type will use the state pattern and will hold a value that will be one of three state objects representing the various states a post can be in, draft, waiting for review or published.
|
|
|
|
Changing from one state change in response to the methods called by our library users on the `Post` instance.
|
|
|
|
They don't have to manage the state changes directly.
|
|
|
|
Users can't make a mistake with the states, like publishing a post before it is reviewed.
|
|
|
|
## Defining `Post` and Creating a New Instance in the Draft State
|
|
First we need a public `Post` struct that holds some content.
|
|
|
|
so we will start with the definition of the struct and the associated public `new` function to create an instance of `Post`.
|
|
|
|
This is shown below.
|
|
|
|
We will also make a private `State` trait that will define the behavior that all state objects for a `Post` must have.
|
|
|
|
`Post` will hold a trait object of `Box<dyn State>` inside an `Option<T>` in a private field named `state` to hold the state object.
|
|
|
|
You will see why the `Option<T>` is necessary.
|
|
```rust
|
|
pub struct Post {
|
|
state: Option<Box<dyn State>>,
|
|
content: String,
|
|
}
|
|
|
|
impl Post {
|
|
pub fn new() -> Post {
|
|
Post {
|
|
state: Some(Box::new(Draft {})),
|
|
content: String::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
trait State {}
|
|
|
|
struct Draft {}
|
|
|
|
impl State for Draft {}
|
|
```
|
|
|
|
The `State` trait defines the behavior shared by different post states.
|
|
|
|
The state objects are `Draft`, `PendingReview` and `Published`, and they will all implement the `State` trait.
|
|
|
|
For now the trait doesn't have any methods.
|
|
|
|
We will start by defining just the `Draft` state because that is the state we want a post to start in.
|
|
|
|
When we create a new `Post`, we set its `state` field to a `Some` value that holds a `Box`.
|
|
|
|
This `Box` points to aa new instance of the `Draft` struct.
|
|
|
|
This ensures whenever we create a new instance of `Post`, it will start out as a draft.
|
|
|
|
Due to the state field of `Post` being private, there is no way to create a `Psot` in any other state.
|
|
|
|
In the `Post::new` function, we set the `content` field to a new empty `String`.
|
|
|
|
## Storing the Text of the Post Content
|
|
Previously we saw that we wanted to be able to call a method named `add_test` and pass it a `&str` that is then added as the text content of the blog post.
|
|
|
|
We implemented this as a method, rather than exposing the `content` field as `pub`, so that later we can implement a method that will control how the `content` field's data is read.
|
|
|
|
The `add_text` method is fairly straightforward, so lets add the implementation below to the `impl Post` block.
|
|
```rust
|
|
impl Post {
|
|
// --snip--
|
|
pub fn add_text(&mut self, text: &str) {
|
|
self.content.push_str(text);
|
|
}
|
|
}
|
|
```
|
|
The `add_text` method takes a mutable reference to `self`.
|
|
|
|
Because we changed the `Post` instance that we are calling `add_text` on.
|
|
|
|
We then can call `push_str` on the `String` in `content` and pass the `text` argument to add to the saved `content`.
|
|
|
|
This behavior doesn't depend on the state the post is in, so it is not part of the state pattern.
|
|
|
|
The `add_text` method doesn't interact with the `state` field at all, but it is part of the behavior we want to support.
|
|
|
|
## Ensuring the Content of a Draft Post Is Empty
|
|
Even after we called `add_text` and added some content to our post, we still want the `content` method to return an empty string slice because the post is still in the draft state.
|
|
|
|
For now we will implement the `content` method with the simplest thing that will fulfill this requirement.
|
|
|
|
Always returning an empty string slice.
|
|
|
|
We will change this late once we implement the ability to change a post's state so that it can be published.
|
|
|
|
Posts so far can only be in the draft state., so the post content should always be empty.
|
|
|
|
Here is a placeholder implementation:
|
|
```rust
|
|
impl Post {
|
|
// --snip--
|
|
pub fn content(&self) -> &str {
|
|
""
|
|
}
|
|
}
|
|
```
|
|
|
|
## Requesting a Review of the Post Changes Its State
|
|
Next we need to add functionality to request a review of a post, which should change its state from `Draft` to `PendingReview`
|
|
|
|
Here is the code that shows this
|
|
```rust
|
|
impl Post {
|
|
// --snip--
|
|
pub fn request_review(&mut self) {
|
|
if let Some(s) = self.state.take() {
|
|
self.state = Some(s.request_review())
|
|
}
|
|
}
|
|
}
|
|
|
|
trait State {
|
|
fn request_review(self: Box<Self>) -> Box<dyn State>;
|
|
}
|
|
|
|
struct Draft {}
|
|
|
|
impl State for Draft {
|
|
fn request_review(self: Box<Self>) -> Box<dyn State> {
|
|
Box::new(PendingReview {})
|
|
}
|
|
}
|
|
|
|
struct PendingReview {}
|
|
|
|
impl State for PendingReview {
|
|
fn request_review(self: Box<Self>) -> Box<dyn State> {
|
|
self
|
|
}
|
|
}
|
|
```
|
|
Here `request_review` is a public method on the `Post` struct, this will take a mutable reference to `self`.
|
|
|
|
Then we call an internal `request_review` method on the current state of `Post`.
|
|
|
|
The second `request_review` method consumes the current state and returns a new state.
|
|
|
|
We add the `request_review` method to the `State` trait.
|
|
|
|
Now all types that implement the trait will now need to implement the `request_review` method.
|
|
|
|
Note, rather than having `self`, `&self` or `&mut self` as the first parameter of the method, we have `self: Box<Self>`.
|
|
|
|
This syntax means the method is only valid when called on a `Box` holding the type.
|
|
|
|
This syntax takes ownership of `Box<Self>`, invalidating the old state so the state value of the `Post` can transform into a new state.
|
|
|
|
In order to consume the old state, the `request_review` method needs to take ownership of the state value.
|
|
|
|
This is where the `Option` in the `state` filed of `Post` comes in.
|
|
|
|
We call the `take` method to take the `Some` value out of the `state` field and leave a `None` in its place, because Rust not allowing us to have unpopulated fields in structs.
|
|
|
|
This lets us move the `state` value out of `Post` rather than borrowing it.
|
|
|
|
Then we will set the post's `state` value to the result of this operation.
|
|
|
|
We need to set `state` to `None` temporarily rather than setting it directly with something like `self.state = self.state.request_review();` to get ownership of the `state` value.
|
|
|
|
This ensures that `Post` can't use the old `state` value after we transformed it into a new state.
|