A code example

In the previous section, we showed the class diagram of the example we will show here. As you can see, we used a model class called Person. It is just a case class with the following definition:

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

Since there will be different formats available in our application, we have defined a common interface that all parsers will implement:

trait Parser[T] {  def parse(file: String): List[T]}

Now, let's have a look at the implementations. First is the CSVParser:

import com.github.tototoshi.csv.CSVReaderclass CSVParser extends Parser[Person] {  override def parse(file: String): List[Person] =    CSVReader.open(new        InputStreamReader(this.getClass.getResourceAsStream(file))).all().map ...

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.