A code example

From the preceding diagram, it is clear that depending on the database client we use, a different connection will be used and created. Now, let's have a look at the code representation for the preceding diagrams. First is the SimpleConnection with its concrete implementations:

trait SimpleConnection {  def getName(): String  def executeQuery(query: String): Unit}class SimpleMysqlConnection extends SimpleConnection {  override def getName(): String = "SimpleMysqlConnection"  override def executeQuery(query: String): Unit = {    System.out.println(s"Executing the query '$query' the MySQL way.")  }}class SimplePgSqlConnection extends SimpleConnection {  override def getName(): String = "SimplePgSqlConnection"  override def executeQuery ...

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.