state_pattern_oop: add reject method
This commit is contained in:
parent
2f1050e854
commit
69a6436139
1 changed files with 13 additions and 0 deletions
|
|
@ -35,6 +35,7 @@ impl Post {
|
|||
trait State {
|
||||
fn request_review(self: Box<Self>) -> Box<dyn State>;
|
||||
fn approve(self: Box<Self>) -> Box<dyn State>;
|
||||
fn reject(self: Box<Self>) -> Box<dyn State>;
|
||||
|
||||
fn content<'a>(&self, _post: &'a Post) -> &'a str {
|
||||
""
|
||||
|
|
@ -51,6 +52,10 @@ impl State for Draft {
|
|||
fn approve(self: Box<Self>) -> Box<dyn State> {
|
||||
self
|
||||
}
|
||||
|
||||
fn reject(self: Box<Self>) -> Box<dyn State> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
struct PendingReview {}
|
||||
|
|
@ -63,6 +68,10 @@ impl State for PendingReview {
|
|||
fn approve(self: Box<Self>) -> Box<dyn State> {
|
||||
Box::new(Published {})
|
||||
}
|
||||
|
||||
fn reject(self: Box<Self>) -> Box<dyn State> {
|
||||
Box::new(Draft {})
|
||||
}
|
||||
}
|
||||
|
||||
struct Published {}
|
||||
|
|
@ -79,6 +88,10 @@ impl State for Published {
|
|||
fn content<'a>(&self, post: &'a Post) -> &'a str {
|
||||
&post.content
|
||||
}
|
||||
|
||||
fn reject(self: Box<Self>) -> Box<dyn State> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue