Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion src/main/kotlin/oop/Memento/CareTaker.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package oop.Memento

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

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

fun getMemento(index: Int) : Memento<T>? = mementoList[index]
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really the best way to do this in Kotlin? I see little sense in wrapping a mutable list 🤔

6 changes: 3 additions & 3 deletions src/test/kotlin/oop/Memento/MementoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MementoTest {

val memento = originator.saveToMemento()

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

@Test
Expand All @@ -35,9 +35,9 @@ class MementoTest {
val originator = Originator(listOf(1,2,3))
val memento = originator.saveToMemento()

careTaker.mementoList.add(memento)
careTaker.addMemento(memento)

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

}