state_pattern_oop: add reject method

This commit is contained in:
laurens 2020-10-11 15:31:27 +02:00
parent 2f1050e854
commit 69a6436139

View file

@ -35,6 +35,7 @@ impl Post {
trait State { trait State {
fn request_review(self: Box<Self>) -> Box<dyn State>; fn request_review(self: Box<Self>) -> Box<dyn State>;
fn approve(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 { 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> { fn approve(self: Box<Self>) -> Box<dyn State> {
self self
} }
fn reject(self: Box<Self>) -> Box<dyn State> {
self
}
} }
struct PendingReview {} struct PendingReview {}
@ -63,6 +68,10 @@ impl State for PendingReview {
fn approve(self: Box<Self>) -> Box<dyn State> { fn approve(self: Box<Self>) -> Box<dyn State> {
Box::new(Published {}) Box::new(Published {})
} }
fn reject(self: Box<Self>) -> Box<dyn State> {
Box::new(Draft {})
}
} }
struct Published {} struct Published {}
@ -79,6 +88,10 @@ impl State for Published {
fn content<'a>(&self, post: &'a Post) -> &'a str { fn content<'a>(&self, post: &'a Post) -> &'a str {
&post.content &post.content
} }
fn reject(self: Box<Self>) -> Box<dyn State> {
self
}
} }
fn main() { fn main() {