![메멘토](/images/patterns/cards/memento-mini.png?id=8b2ea4dc2c5d15775a654808cc9de099)
러스트로 작성된 메멘토
메멘토 패턴은 행동 디자인 패턴입니다. 이 패턴은 객체 상태의 스냅숏을 만든 후 나중에 복원할 수 있도록 합니다.
메멘토는 함께 작동하는 객체의 내부 구조와 스냅숏들 내부에 보관된 데이터를 손상하지 않습니다.
Conceptual example
This is a conceptual example of Memento pattern.
conceptual.rs
trait Memento<T> {
fn restore(self) -> T;
fn print(&self);
}
struct Originator {
state: u32,
}
impl Originator {
pub fn save(&self) -> OriginatorBackup {
OriginatorBackup {
state: self.state.to_string(),
}
}
}
struct OriginatorBackup {
state: String,
}
impl Memento<Originator> for OriginatorBackup {
fn restore(self) -> Originator {
Originator {
state: self.state.parse().unwrap(),
}
}
fn print(&self) {
println!("Originator backup: '{}'", self.state);
}
}
fn main() {
let mut history = Vec::<OriginatorBackup>::new();
let mut originator = Originator { state: 0 };
originator.state = 1;
history.push(originator.save());
originator.state = 2;
history.push(originator.save());
for moment in history.iter() {
moment.print();
}
let originator = history.pop().unwrap().restore();
println!("Restored to state: {}", originator.state);
let originator = history.pop().unwrap().restore();
println!("Restored to state: {}", originator.state);
}
Output
Originator backup: '1' Originator backup: '2' Restored to state: 2 Restored to state: 1
Serde serialization framework
A common way to make a structure serializable is to derive Serialize
and Deserialize
traits from the serde serialization framework. Then an object of serializable type can be converted to many different formats, e.g. JSON with serde_json crate.
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Originator {
state: u32,
}
serde.rs
use serde::{Deserialize, Serialize};
/// An object to be stored. It derives a default
/// `Serialize` and `Deserialize` trait implementation, which
/// allows to convert it into many different formats (e.g. JSON).
#[derive(Serialize, Deserialize)]
struct Originator {
state: u32,
}
impl Originator {
/// Serializes an originator into a string of JSON format.
pub fn save(&self) -> String {
serde_json::to_string(self).unwrap()
}
/// Deserializes an originator into a string of JSON format.
pub fn restore(json: &str) -> Self {
serde_json::from_str(json).unwrap()
}
}
fn main() {
// A stack of mementos.
let mut history = Vec::<String>::new();
let mut originator = Originator { state: 0 };
originator.state = 1;
history.push(originator.save());
originator.state = 2;
history.push(originator.save());
for moment in history.iter() {
println!("{}", moment);
}
let originator = Originator::restore(&history.pop().unwrap());
println!("Restored to state: {}", originator.state);
let originator = Originator::restore(&history.pop().unwrap());
println!("Restored to state: {}", originator.state);
}
Output
{"state":1} {"state":2} Restored to state: 2 Restored to state: 1