Prototype Pattern
The prototype pattern is a creational pattern that allows cloning of objects, even complex ones, without coupling to their specific classes. This pattern is particularly useful when the cost of creating a new instance of a class is expensive or complicated.
We can implement the pattern using the Clone
trait.
#[derive(Clone)] struct Prototype { field1: String, field2: i32, } impl Prototype { fn new(field1: String, field2: i32) -> Self { Prototype { field1, field2 } } fn clone_prototype(&self) -> Self { self.clone() } } fn main() { let original = Prototype::new(String::from("Prototype"), 42); let cloned = original.clone_prototype(); println!("Original: {} - {}", original.field1, original.field2); println!("Cloned: {} - {}", cloned.field1, cloned.field2); }
- We define a
Prototype
struct with two fields. - We derive the
Clone
trait for thePrototype
struct, which provides theclone
method. - We implement a
clone_prototype
method that clones the current instance. - In the
main
function, we create an instance ofPrototype
and then clone it using theclone_prototype
method.
This demonstrates how the Prototype pattern can be implemented in Rust using the Clone
trait.