April 2018
Intermediate to advanced
396 pages
11h 8m
English
Let's have a look at our example from the source point of view. The following code listing shows the factory hierarchy:
trait DatabaseConnectorFactory { def connect(): SimpleConnection}class MySqlFactory extends DatabaseConnectorFactory { override def connect(): SimpleConnection = new SimpleMysqlConnection}class PgSqlFactory extends DatabaseConnectorFactory { override def connect(): SimpleConnection = new SimplePgSqlConnection}
We can then use our factory by passing it to a class, that will call the required methods. Here is an example similar to the one we showed for the factory method design pattern:
class DatabaseClient(connectorFactory: DatabaseConnectorFactory) { def executeQuery(query: String): Unit = { val connection ...
Read now
Unlock full access