state_pattern_oop: add approval count of 2
This commit is contained in:
parent
69a6436139
commit
3aa19cc04b
1 changed files with 25 additions and 4 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
pub struct Post {
|
pub struct Post {
|
||||||
state: Option<Box<dyn State>>,
|
state: Option<Box<dyn State>>,
|
||||||
content: String,
|
content: String,
|
||||||
|
|
@ -46,7 +48,7 @@ struct Draft {}
|
||||||
|
|
||||||
impl State for Draft {
|
impl State for Draft {
|
||||||
fn request_review(self: Box<Self>) -> Box<dyn State> {
|
fn request_review(self: Box<Self>) -> Box<dyn State> {
|
||||||
Box::new(PendingReview {})
|
Box::new(PendingReview::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn approve(self: Box<Self>) -> Box<dyn State> {
|
fn approve(self: Box<Self>) -> Box<dyn State> {
|
||||||
|
|
@ -58,7 +60,17 @@ impl State for Draft {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct PendingReview {}
|
struct PendingReview {
|
||||||
|
approve_count: RefCell<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PendingReview {
|
||||||
|
pub fn new() -> PendingReview {
|
||||||
|
PendingReview {
|
||||||
|
approve_count: RefCell::new(0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl State for PendingReview {
|
impl State for PendingReview {
|
||||||
fn request_review(self: Box<Self>) -> Box<dyn State> {
|
fn request_review(self: Box<Self>) -> Box<dyn State> {
|
||||||
|
|
@ -66,7 +78,12 @@ impl State for PendingReview {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn approve(self: Box<Self>) -> Box<dyn State> {
|
fn approve(self: Box<Self>) -> Box<dyn State> {
|
||||||
Box::new(Published {})
|
self.approve_count.replace_with(|&mut old| old + 1);
|
||||||
|
if *self.approve_count.borrow() >= 2 {
|
||||||
|
Box::new(Published {})
|
||||||
|
} else {
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reject(self: Box<Self>) -> Box<dyn State> {
|
fn reject(self: Box<Self>) -> Box<dyn State> {
|
||||||
|
|
@ -99,10 +116,14 @@ fn main() {
|
||||||
|
|
||||||
post.add_text("I ate a salad for lunch today");
|
post.add_text("I ate a salad for lunch today");
|
||||||
assert_eq!("", post.content());
|
assert_eq!("", post.content());
|
||||||
|
post.approve(); // Shouldn't cause approval to go forward!
|
||||||
|
|
||||||
post.request_review();
|
post.request_review();
|
||||||
assert_eq!("", post.content());
|
assert_eq!("", post.content());
|
||||||
|
|
||||||
post.approve();
|
post.approve(); // First valid approval, we need two so the content should still be empty
|
||||||
|
assert_eq!("", post.content());
|
||||||
|
|
||||||
|
post.approve(); // Last approval
|
||||||
assert_eq!("I ate a salad for lunch today", post.content());
|
assert_eq!("I ate a salad for lunch today", post.content());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue