April 2018
Intermediate to advanced
396 pages
11h 8m
English
In this subsection, we will go through the text editor code one step at a time and see how the memento design pattern could be implemented in Scala.
First of all, let's see the Caretaker, Memento, and Originator traits:
trait Memento[T] { protected val state: T def getState(): T = state}trait Caretaker[T] { val states: mutable.Stack[Memento[T]] = mutable.Stack[Memento[T]]()}trait Originator[T] { def createMemento: Memento[T] def restore(memento: Memento[T])}
We have used generics, and this allows us to reuse those traits multiple times when we want to implement the memento design pattern. Now, let's take a look at the specific implementations of the traits that are necessary in our application:
class TextEditor extends Originator[ ...
Read now
Unlock full access