Skip to content
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

Added Memento pattern #40

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
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Kotlin OOP and FP Design Patterns
* [ ] [Interpreter](#interpreter)
* [ ] [Iterator](#iterator)
* [ ] [Mediator](#mediator)
* [ ] [Memento](#memento)
* [x] [Memento](#memento)
* [ ] [Null Object](#null-object)
* [x] [Observer](#observer)
* [x] [State](#state)
Expand Down Expand Up @@ -174,12 +174,40 @@ Mediator

**In progress**

Memento
[Memento](/src/main/kotlin/oop/Memento)
---------

> It captures and externalizes an object's internal state so it can get back to this state later without violating encapsulation

**In progress**
### Example

```kotlin
data class Memento<out T>(val state: T)

class Originator<T>(var state: T) {
fun saveToMemento() = Memento(state)
fun loadFromMemento(memento: Memento<T>) {
state = memento.state
}
}

class CareTaker<T> {
val mementoList = mutableListOf<Memento<T>>()
}
```

### Usage

```kotlin
val changingClass = Originator("Hello world")
val states = CareTaker<String>()

changingClass.state += "!"
states.mementoList.add(changingClass.saveToMemento())

changingClass.state = "I've made a mistake :("
changingClass.loadFromMemento(states.mementoList[0])
```

Null Object
-----------
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/oop/Memento/CareTaker.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package oop.Memento

class CareTaker<T> {
private val mementoList = mutableListOf<Memento<T>>()

fun addMemento(memento: Memento<T>) {
mementoList.add(memento)
}

fun getMemento(index: Int) : Memento<T>? = mementoList[index]
}
3 changes: 3 additions & 0 deletions src/main/kotlin/oop/Memento/Memento.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package oop.Memento

data class Memento<out T>(val state: T)
8 changes: 8 additions & 0 deletions src/main/kotlin/oop/Memento/Originator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package oop.Memento

class Originator<T>(var state : T) {
fun saveToMemento() = Memento(state)
fun loadFromMemento(memento: Memento<T>) {
state = memento.state
}
}
43 changes: 43 additions & 0 deletions src/test/kotlin/oop/Memento/MementoTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package oop.Memento

import com.natpryce.hamkrest.Matcher
import com.natpryce.hamkrest.assertion.assert
import org.junit.Test

class MementoTest {

fun <T> `is`(list: List<T>): Matcher<List<T>> = Matcher(List<T>::equals, list)
fun <T> `is`(list: Memento<T>): Matcher<Memento<T>> = Matcher(Memento<T>::equals, list)

@Test
fun `memento state should be equal to last originator state`() {
val originator = Originator(emptyList<Int>())
originator.state = listOf(1,2,3)

val memento = originator.saveToMemento()

assert.that(originator.state, `is`(memento.state))
}

@Test
fun `originator state should be equal to last memento loaded`() {
val memento = Memento(listOf(1,2,3))
val originator = Originator(emptyList<Int>())

originator.loadFromMemento(memento)

assert.that(originator.state, `is`(memento.state))
}

@Test
fun `care taker should store mementos`() {
val careTaker = CareTaker<List<Int>>()
val originator = Originator(listOf(1,2,3))
val memento = originator.saveToMemento()

careTaker.addMemento(memento)

assert.that(careTaker.getMemento(0)!!, `is`(memento))
}

}