Code example

Let's go through the code that represents the preceding diagram and take a look at what it does. First of all, our model Person class:

case class Person(name: String, age: Int, address: String)

There is nothing special about it. Now, let's move on to the interesting part—the DataFinder class:

abstract class DataFinder[T, Y] {  def find(f: T => Option[Y]): Option[Y] =    try {      val data = readData()      val parsed = parse(data)      f(parsed)    } finally {      cleanup()    }  def readData(): Array[Byte]  def parse(data: Array[Byte]): T  def cleanup()}

We have used generics in order to make this class usable for various types. As you can see in the preceding code, three of the methods of the DataFinder class have no implementations, but they are still referred ...

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.