mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2026-08-19 12:43:28 +08:00
Updated 'src/oop/implementing.md'.
This commit is contained in:
@@ -1,74 +1,43 @@
|
||||
pub struct Post {
|
||||
state: Option<Box<dyn State>>,
|
||||
content: String,
|
||||
}
|
||||
|
||||
pub struct DraftPost {
|
||||
content: String,
|
||||
}
|
||||
|
||||
impl Post {
|
||||
pub fn new() -> Post {
|
||||
Post {
|
||||
state: Some(Box::new(Draft {})),
|
||||
pub fn new() -> DraftPost {
|
||||
DraftPost {
|
||||
content: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn content(&self) -> &str {
|
||||
&self.content
|
||||
}
|
||||
}
|
||||
|
||||
impl DraftPost {
|
||||
pub fn add_text(&mut self, text: &str) {
|
||||
self.content.push_str(text);
|
||||
}
|
||||
|
||||
pub fn content(&self) -> &str {
|
||||
self.state.as_ref().unwrap().content(self)
|
||||
}
|
||||
|
||||
pub fn request_review(&mut self) {
|
||||
if let Some(s) = self.state.take() {
|
||||
self.state = Some(s.request_review())
|
||||
}
|
||||
|
||||
pub fn approve(&mut self) {
|
||||
if let Some(s) = self.state.take() {
|
||||
self.state = Some(s.approve())
|
||||
}
|
||||
pub fn request_review(self) -> PendingReviewPost {
|
||||
PendingReviewPost {
|
||||
content: self.content,
|
||||
}
|
||||
}
|
||||
|
||||
trait State {
|
||||
fn request_review(self: Box<Self>) -> Box<dyn State>;
|
||||
fn approve(self: Box<Self>) -> Box<dyn State>;
|
||||
}
|
||||
}
|
||||
|
||||
struct Draft {}
|
||||
|
||||
impl State for Draft {
|
||||
fn request_review(self: Box<Self>) -> Box<dyn State> {
|
||||
Box::new(PendingReview {})
|
||||
}
|
||||
|
||||
fn approve(self: Box<Self>) -> Box<dyn State> {
|
||||
self
|
||||
}
|
||||
pub struct PendingReviewPost {
|
||||
content: String,
|
||||
}
|
||||
|
||||
struct PendingReview {}
|
||||
|
||||
impl State for PendingReview {
|
||||
fn request_review(self: Box<Self>) -> Box<dyn State> {
|
||||
self
|
||||
}
|
||||
|
||||
fn approve(self: Box<Self>) -> Box<dyn State> {
|
||||
Box::new(Published {})
|
||||
}
|
||||
}
|
||||
|
||||
struct Published {}
|
||||
|
||||
impl State for Published {
|
||||
fn request_review(self: Box<Self>) -> Box<dyn State> {
|
||||
self
|
||||
}
|
||||
|
||||
fn approve(self: Box<Self>) -> Box<dyn State> {
|
||||
self
|
||||
impl PendingReviewPost {
|
||||
pub fn approve(self) -> Post {
|
||||
Post {
|
||||
content: self.content,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
21
projects/blog/src/main.rs
Normal file
21
projects/blog/src/main.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use blog::Post;
|
||||
use std::any::type_name;
|
||||
|
||||
fn main() {
|
||||
let mut post = Post::new();
|
||||
println! ("{}", type_name_of_val(&post));
|
||||
|
||||
post.add_text("今天午饭我吃了沙拉。");
|
||||
|
||||
let post = post.request_review();
|
||||
println! ("{}", type_name_of_val(&post));
|
||||
|
||||
let post = post.approve();
|
||||
println! ("{}", type_name_of_val(&post));
|
||||
|
||||
assert_eq! ("今天午饭我吃了沙拉。", post.content());
|
||||
}
|
||||
|
||||
fn type_name_of_val<T>(_: &T) -> &'static str {
|
||||
type_name::<T>()
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "neo_simple_blog"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
@@ -1,43 +0,0 @@
|
||||
pub struct Post {
|
||||
content: String,
|
||||
}
|
||||
|
||||
pub struct DraftPost {
|
||||
content: String,
|
||||
}
|
||||
|
||||
impl Post {
|
||||
pub fn new() -> DraftPost {
|
||||
DraftPost {
|
||||
content: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn content(&self) -> &str {
|
||||
&self.content
|
||||
}
|
||||
}
|
||||
|
||||
impl DraftPost {
|
||||
pub fn add_text(&mut self, text: &str) {
|
||||
self.content.push_str(text);
|
||||
}
|
||||
|
||||
pub fn request_review(self) -> PendingReviewPost {
|
||||
PendingReviewPost {
|
||||
content: self.content,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PendingReviewPost {
|
||||
content: String,
|
||||
}
|
||||
|
||||
impl PendingReviewPost {
|
||||
pub fn approve(self) -> Post {
|
||||
Post {
|
||||
content: self.content,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
#![allow(dead_code)]
|
||||
#![allow(unused_variables)]
|
||||
|
||||
use neo_simple_blog::Post;
|
||||
|
||||
fn main() {
|
||||
let mut post = Post::new();
|
||||
|
||||
post.add_text("这是一个博客帖子。");
|
||||
|
||||
let post = post.request_review();
|
||||
let post = post.approve();
|
||||
|
||||
assert_eq! ("这是一个博客帖子。", post.content());
|
||||
}
|
||||
Reference in New Issue
Block a user