Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DesignPatterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This project explores various design patterns and their implementations in moder

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

### Structural Design Patterns 🎁
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package pl.mperor.lab.java.design.pattern.creational.prototype;

public record DNA(String sequence) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package pl.mperor.lab.java.design.pattern.creational.prototype;

import java.time.Year;
import java.util.function.Function;

public class Sheep {

private String name;
private DNA dna;
private Year yearOfBirth;
private Breed breed;

private Sheep(Sheep sheep) {
this.name = sheep.name;
this.dna = sheep.dna;
this.yearOfBirth = Year.now();
this.breed = sheep.breed;
}

public Sheep(String name, DNA dna, Year yearOfBirth, Breed breed) {
this.name = name;
this.dna = dna;
this.yearOfBirth = yearOfBirth;
this.breed = breed;
}

String getName() {
return name;
}

void setName(String name) {
this.name = name;
}

DNA getDna() {
return dna;
}

Year getYearOfBirth() {
return yearOfBirth;
}

Breed getBreed() {
return breed;
}

public Sheep clone(Function<CloneBuilder, Sheep> cloning) {
return cloning.apply(new CloneBuilder(new Sheep(this)));
}

public class CloneBuilder {
private Sheep clone;

public CloneBuilder(Sheep clone) {
this.clone = clone;
}

public CloneBuilder name(String name) {
clone.name = name;
return this;
}

public Sheep build() {
return clone;
}
}

enum Breed {
MERINO,
SUFFOLK,
DORSET,
HAMPSHIRE
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pl.mperor.lab.java.design.pattern.creational.prototype;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.time.Year;

public class SheepPrototypeTest {

@Test
public void testSheepCloning() {
Sheep originalSheep = new Sheep("Dolly", new DNA("AGCT"), Year.of(2010), Sheep.Breed.MERINO);
Sheep clonedSheep = originalSheep.clone(builder -> builder
.name("Molly")
.build()
);
Assertions.assertNotSame(originalSheep, clonedSheep);
Assertions.assertSame(originalSheep.getDna(), clonedSheep.getDna());
Assertions.assertSame(originalSheep.getBreed(), clonedSheep.getBreed());
Assertions.assertNotEquals(originalSheep.getYearOfBirth(), clonedSheep.getYearOfBirth());
}
}
Loading