Skip to content

Add Prototype pattern #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Inspired by [Design-Patterns-In-Swift](https://github.com/ochococo/Design-Patter
* [Builder / Assembler](#builder--assembler)
* [Factory Method](#factory-method)
* [Singleton](#singleton)
* [Prototype](#prototype)
* [Abstract Factory](#abstract-factory)
* [Structural Patterns](#structural)
* [Adapter](#adapter)
Expand Down Expand Up @@ -750,6 +751,53 @@ Printing with object: PrinterDriver@6ff3c5b5
Printing with object: PrinterDriver@6ff3c5b5
```

[Prototype](/patterns/src/test/kotlin/Prototype.kt)
------------

The prototype is used to copy existing objects without making your code dependent on their classes.

#### Example:

```kotlin
data class Personal(
val name: String,
val age: Int,
val country: String,
val gender: Gender
)

enum class Gender {
FEMALE, MALE, OTHER
}

fun Any.getObjectSignature(): String = "${this.javaClass.name}@${Integer.toHexString(System.identityHashCode(this))}"
```

#### Usage

```kotlin
val personal = Personal(
name = "Emanuel",
age = 20,
country = "Brazil",
gender = Gender.MALE
)
val personalClone = personal.copy()

println("personal printing with values $personal and with object ${personal.getObjectSignature()}")
println("personalClone printing with values $personalClone and with object ${personalClone.getObjectSignature()}")

assertThat(personal).isNotSameAs(personalClone)
assertThat(personal).isEqualTo(personalClone)
```

#### Output

```
personal printing with values Personal(name=Emanuel, age=20, country=Brazil, gender=MALE) and with object Personal@47f1ea23
personalClone printing with values Personal(name=Emanuel, age=20, country=Brazil, gender=MALE) and with object Personal@18682904
```

[Abstract Factory](/patterns/src/test/kotlin/AbstractFactory.kt)
-------------------

Expand Down
36 changes: 36 additions & 0 deletions patterns/src/test/kotlin/Prototype.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

data class Personal(
val name: String,
val age: Int,
val country: String,
val gender: Gender
)

enum class Gender {
FEMALE, MALE, OTHER
}

fun Any.getObjectSignature(): String = "${this.javaClass.name}@${Integer.toHexString(System.identityHashCode(this))}"

class PrototypeTest {

@Test
fun Prototype() {
val personal = Personal(
name = "Emanuel",
age = 20,
country = "Brazil",
gender = Gender.MALE
)
val personalClone = personal.copy()

println("personal printing with values $personal and with object ${personal.getObjectSignature()}")
println("personalClone printing with values $personalClone and with object ${personalClone.getObjectSignature()}")

assertThat(personal).isNotSameAs(personalClone)
assertThat(personal).isEqualTo(personalClone)

}
}