Skip to content

Commit

Permalink
prototype code repo
Browse files Browse the repository at this point in the history
  • Loading branch information
saltukalakus committed Jan 1, 2025
1 parent 8108238 commit 25a6f33
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
24 changes: 1 addition & 23 deletions src/patterns/creational/prototype.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,7 @@ The prototype pattern is a creational pattern that allows cloning of objects, ev
We can implement the pattern using the `Clone` trait. Here is an example:

```rust
#[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);
}
{{#include prototype/src/main.rs}}
```

- We define a `Prototype` struct with two fields.
Expand Down
7 changes: 7 additions & 0 deletions src/patterns/creational/prototype/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/patterns/creational/prototype/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "prototype_pattern"
version = "0.1.0"
edition = "2018"

[dependencies]
23 changes: 23 additions & 0 deletions src/patterns/creational/prototype/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#[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);
}

0 comments on commit 25a6f33

Please sign in to comment.