April 2018
Intermediate to advanced
396 pages
11h 8m
English
The prototype design pattern is really easy to implement in Scala. We can just use one of the language features. Since the prototype design pattern really resembles how cells in biology divide, let's use a cell as an example:
/** * Represents a bio cell */case class Cell(dna: String, proteins: List[String])
In Scala, all case classes have a copy method, which returns a new instance that is cloned from the original one. It can also change some of the original properties while copying. Here is some example usage of our cell:
object PrototypeExample { def main(args: Array[String]): Unit = { val initialCell = Cell("abcd", List("protein1", "protein2")) val copy1 = initialCell.copy() val copy2 = initialCell.copy() val copy3 = initialCell.copy(dna ...
Read now
Unlock full access