April 2018
Intermediate to advanced
396 pages
11h 8m
English
Now, let's have a closer look at the code that implements the preceding class diagram. The first thing we need to define is the interface (using a Scala trait):
trait FileReader { def readFileContents(): String}
We then create two classes that implement it—FileReaderReal and FileReaderProxy. First, let's see how the former implements the file read as it has nothing of real significance:
class FileReaderReal(filename: String) extends FileReader { val contents = { val stream = this.getClass.getResourceAsStream(filename) val reader = new BufferedReader( new InputStreamReader( stream ) ) try { reader.lines().iterator().asScala.mkString (System.getProperty("line.separator")) } finally { reader.close() stream.close() } } System.out ...
Read now
Unlock full access