我想学习设计模式使用 rust 语言。从这段代码中它可以正常工作,但它使用动态调度。如何更改代码以使用静态调度?谢谢!
trait Shape {
fn draw(&self);
}
enum ShapeType {
Rectangle,
Circle,
}
struct Rectangle {}
impl Shape for Rectangle {
fn draw(&self) {
println!("draw a rectangle!");
}
}
struct Circle {}
impl Shape for Circle {
fn draw(&self) {
println!("draw a circle!");
}
}
struct ShapeFactory;
impl ShapeFactory {
fn new_shape(s: &ShapeType) -> Box<dyn Shape> {
match s {
ShapeType::Circle => Box::new(Circle {}),
ShapeType::Rectangle => Box::new(Rectangle {}),
}
}
}
fn main() {
let shape = ShapeFactory::new_shape(&ShapeType::Circle);
shape.draw(); // output: draw a circle!
let shape = ShapeFactory::new_shape(&ShapeType::Rectangle);
shape.draw(); // output: draw a rectangle!
}