1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
/// # 设计模式 /// /// Base usage: 建造者模式 /// /// Rust常用设计模式之一,标准库中std::process::Command就是典型 /// /// ```rust /// struct Circle { /// x: f64, /// y: f64, /// radius: f64, /// } /// struct CircleBuilder { /// x: f64, /// y: f64, /// radius: f64, /// } /// impl Circle { /// fn area(&self) -> f64 { /// std::f64::consts::PI * (self.radius * self.radius) /// } /// fn new() -> CircleBuilder { /// CircleBuilder { /// x: 0.0, y: 0.0, radius: 1.0, /// } /// } /// } /// impl CircleBuilder { /// fn x(&mut self, coordinate: f64) -> &mut CircleBuilder { /// self.x = coordinate; /// self /// } /// fn y(&mut self, coordinate: f64) -> &mut CircleBuilder { /// self.y = coordinate; /// self /// } /// fn radius(&mut self, radius: f64) -> &mut CircleBuilder { /// self.radius = radius; /// self /// } /// fn build(&self) -> Circle { /// Circle { /// x: self.x, y: self.y, radius: self.radius, /// } /// } /// } /// fn main() { /// let c = Circle::new() /// .x(1.0).y(2.0).radius(2.0) /// . build(); /// assert_eq!(c.area(), 12.566370614359172); // 可能不同机器的值有所差异 /// assert_eq!(c.x, 1.0); /// assert_eq!(c.y, 2.0); /// } /// ``` /// /// Base usage: 访问者模式 /// /// Rust源码中还包含两个案例: /// 1. rustc编译器源码中AST处理 /// 2. Serde的架构是访问者模式 /// /// ```rust /// use std::any::Any; /// trait HouseElement { /// fn accept(&self, visitor: &HouseElementVisitor); /// fn as_any(&self) -> &Any; /// } /// trait HouseElementVisitor { /// fn visit(&self, element: &HouseElement); /// } /// struct House { /// components: Vec<Box<HouseElement>>, /// } /// impl House { /// fn new() -> Self { /// House { /// components: vec![Box::new(Livingroom::new())], /// } /// } /// } /// impl HouseElement for House { /// fn accept(&self, visitor: &HouseElementVisitor) { /// for component in self.components.iter() { /// component.accept(visitor); /// } /// visitor.visit(self); /// } /// fn as_any(&self) -> &Any { self } /// } /// /// struct Livingroom; /// impl Livingroom { /// fn new() -> Self { Livingroom } /// } /// impl HouseElement for Livingroom { /// fn accept(&self, visitor: &HouseElementVisitor) { /// visitor.visit(self); /// } /// fn as_any(&self) -> &Any { self } /// } /// /// struct HouseElementListVisitor; /// impl HouseElementListVisitor { /// fn new() -> Self { HouseElementListVisitor } /// } /// /// impl HouseElementVisitor for HouseElementListVisitor { /// fn visit(&self, element: &HouseElement) { /// match element.as_any() { /// house if house.is::<House>() => println!("Visiting the house..."), /// living if living.is::<Livingroom>() => println!("Visiting the Living room..."), /// _ => {} /// } /// } /// } /// struct HouseElementDemolishVisitor; /// impl HouseElementDemolishVisitor { /// pub fn new() -> Self { /// HouseElementDemolishVisitor /// } /// } /// impl HouseElementVisitor for HouseElementDemolishVisitor { /// fn visit(&self, element: &HouseElement) { /// match element.as_any() { /// house if house.is::<House>() => println!("Annihilating the house...!!!"), /// living if living.is::<Livingroom>() => println!("Bombing the Living room...!!!"), /// _ => {} /// } /// } /// } /// /// fn main() { /// let house = House::new(); /// // simply print out the house elements /// house.accept(&HouseElementListVisitor::new()); /// println!(); /// // do something with the elements of a house /// house.accept(&HouseElementDemolishVisitor::new()); /// } /// ``` /// /// Base usage: RAII 模式 /// /// ```rust /// /* /// 利用Rust的ownership(linear/affine type)来设计接口 /// /// 存在的问题: /// /// 1. Letter有可能复制多份到多个信封(envelope)里,不安全 /// 2. 信封里有可能有信,也有可能没有信;或者同一个信封装多次不同的信件,不安全 /// 3. 有没有把信交给邮车也是无法保证,不安全 /// /// */ /// /// #[derive(Clone)] /// pub struct Letter { /// text: String, /// } /// pub struct Envelope { /// letter: Option<Letter>, /// } /// pub struct PickupLorryHandle { /// done: bool, /// } /// impl Letter { /// pub fn new(text: String) -> Self { /// Letter {text: text} /// } /// } /// impl Envelope { /// pub fn wrap(&mut self, letter: &Letter){ /// self.letter = Some(letter.clone()); /// } /// } /// pub fn buy_prestamped_envelope() -> Envelope { /// Envelope {letter: None} /// } /// impl PickupLorryHandle { /// pub fn pickup(&mut self, envelope: &Envelope) { /// /*give letter*/ /// } /// pub fn done(&mut self) { /// self.done = true; /// println!("sent"); /// } /// } /// pub fn order_pickup() -> PickupLorryHandle { /// PickupLorryHandle {done: false , /* other handles */} /// } /// fn main(){ /// let letter = Letter::new(String::from("Dear RustFest")); /// let mut envelope = buy_prestamped_envelope(); /// envelope.wrap(&letter); /// let mut lorry = order_pickup(); /// lorry.pickup(&envelope); /// lorry.done(); /// } /// /// pub fn builder_pattern(){ /// unimplemented!(); /// } /// ``` /// /// Base usage: 【重构】RAII 模式 /// /// ```rust /// /* /// 利用Rust的ownership(linear/affine type)来设计接口 /// /// Refact /// /// 1. 去掉letter的Clone,使用所有权保证其唯一性;并且信封的wrap方法只接受获得所有权的信封,而非引用 /// 2. 使用类型系统来保证信封的唯一性 /// 3. 使用RAII机制来管理收尾逻辑,比如实现Drop /// /// 其他示例: /// /// 1. http response /// 2. steaming engine /// /// */ /// /// // #[derive(Clone)] /// pub struct Letter { /// text: String, /// } /// pub struct EmptyEnvelope {} /// pub struct ClosedEnvelope { /// letter: Letter, /// } /// pub struct PickupLorryHandle { /// done: bool, /// } /// impl Letter { /// pub fn new(text: String) -> Self { /// Letter {text: text} /// } /// } /// impl EmptyEnvelope { /// pub fn wrap(self, letter: Letter) -> ClosedEnvelope { /// ClosedEnvelope {letter: letter} /// } /// } /// pub fn buy_prestamped_envelope() -> EmptyEnvelope { /// EmptyEnvelope {} /// } /// impl PickupLorryHandle { /// pub fn pickup(&mut self, envelope: ClosedEnvelope) { /// /*give letter*/ /// } /// pub fn done(self) {} /// } /// impl Drop for PickupLorryHandle { /// fn drop(&mut self) { /// println!("sent"); /// } /// } /// pub fn order_pickup() -> PickupLorryHandle { /// PickupLorryHandle {done: false , /* other handles */} /// } /// fn main(){ /// let letter = Letter::new(String::from("Dear RustFest")); /// let envelope = buy_prestamped_envelope(); /// let closed_envelope = envelope.wrap(letter); /// let mut lorry = order_pickup(); /// lorry.pickup(closed_envelope); /// } /// ``` pub fn design_patterns(){ unimplemented!(); }