Skip to content

Commit de02911

Browse files
authored
Add prototype design pattern with test case (#50)
1 parent 41c070f commit de02911

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

DesignPatterns/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This project explores various design patterns and their implementations in moder
1515

1616
- [Factory](src/main/java/pl/mperor/lab/java/design/pattern/creational/factory) 🏭
1717
- [Builder](src/main/java/pl/mperor/lab/java/design/pattern/creational/builder) 🏗️
18+
- [Prototype](src/main/java/pl/mperor/lab/java/design/pattern/creational/prototype) 🧬
1819
- [Singleton](src/main/java/pl/mperor/lab/java/design/pattern/creational/singleton) 1️⃣
1920

2021
### Structural Design Patterns 🎁
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package pl.mperor.lab.java.design.pattern.creational.prototype;
2+
3+
public record DNA(String sequence) {
4+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package pl.mperor.lab.java.design.pattern.creational.prototype;
2+
3+
import java.time.Year;
4+
import java.util.function.Function;
5+
6+
public class Sheep {
7+
8+
private String name;
9+
private DNA dna;
10+
private Year yearOfBirth;
11+
private Breed breed;
12+
13+
private Sheep(Sheep sheep) {
14+
this.name = sheep.name;
15+
this.dna = sheep.dna;
16+
this.yearOfBirth = Year.now();
17+
this.breed = sheep.breed;
18+
}
19+
20+
public Sheep(String name, DNA dna, Year yearOfBirth, Breed breed) {
21+
this.name = name;
22+
this.dna = dna;
23+
this.yearOfBirth = yearOfBirth;
24+
this.breed = breed;
25+
}
26+
27+
String getName() {
28+
return name;
29+
}
30+
31+
void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
DNA getDna() {
36+
return dna;
37+
}
38+
39+
Year getYearOfBirth() {
40+
return yearOfBirth;
41+
}
42+
43+
Breed getBreed() {
44+
return breed;
45+
}
46+
47+
public Sheep clone(Function<CloneBuilder, Sheep> cloning) {
48+
return cloning.apply(new CloneBuilder(new Sheep(this)));
49+
}
50+
51+
public class CloneBuilder {
52+
private Sheep clone;
53+
54+
public CloneBuilder(Sheep clone) {
55+
this.clone = clone;
56+
}
57+
58+
public CloneBuilder name(String name) {
59+
clone.name = name;
60+
return this;
61+
}
62+
63+
public Sheep build() {
64+
return clone;
65+
}
66+
}
67+
68+
enum Breed {
69+
MERINO,
70+
SUFFOLK,
71+
DORSET,
72+
HAMPSHIRE
73+
}
74+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package pl.mperor.lab.java.design.pattern.creational.prototype;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.time.Year;
7+
8+
public class SheepPrototypeTest {
9+
10+
@Test
11+
public void testSheepCloning() {
12+
Sheep originalSheep = new Sheep("Dolly", new DNA("AGCT"), Year.of(2010), Sheep.Breed.MERINO);
13+
Sheep clonedSheep = originalSheep.clone(builder -> builder
14+
.name("Molly")
15+
.build()
16+
);
17+
Assertions.assertNotSame(originalSheep, clonedSheep);
18+
Assertions.assertSame(originalSheep.getDna(), clonedSheep.getDna());
19+
Assertions.assertSame(originalSheep.getBreed(), clonedSheep.getBreed());
20+
Assertions.assertNotEquals(originalSheep.getYearOfBirth(), clonedSheep.getYearOfBirth());
21+
}
22+
}

0 commit comments

Comments
 (0)