Traits example:
cala> println("Step 1: Create a trait which will define the methods for a data access layer")
Step 1: Create a trait which will define the methods for a data access layer
scala> trait DonutShoppingCartDao {
|
| def add(donutName: String): Long
|
| def update(donutName: String): Boolean
|
| def search(donutName: String): String
|
| def delete(donutName: String): Boolean
|
| }
defined trait DonutShoppingCartDao
scala> println("\nStep 2: Create a DonutShoppingCart class which extends the trait from Step 1 and implements its methods")
Step 2: Create a DonutShoppingCart class which extends the trait from Step 1 and implements its methods
scala> class DonutShoppingCart extends DonutShoppingCartDao {
|
| override def add(donutName: String): Long = {
| println(s"DonutShoppingCart-> add method -> donutName: $donutName")
| 1
| }
|
| override def update(donutName: String): Boolean = {
| println(s"DonutShoppingCart-> update method -> donutName: $donutName")
| true
| }
|
| override def search(donutName: String): String = {
| println(s"DonutShoppingCart-> search method -> donutName: $donutName")
| donutName
| }
|
| override def delete(donutName: String): Boolean = {
| println(s"DonutShoppingCart-> delete method -> donutName: $donutName")
| true
| }
| }
defined class DonutShoppingCart
scala>
scala> println("\nStep 3: Create an instance of DonutShoppingCart and call the add, update, search and delete methods")
Step 3: Create an instance of DonutShoppingCart and call the add, update, search and delete methods
scala> val donutShoppingCart1: DonutShoppingCart = new DonutShoppingCart()
donutShoppingCart1: DonutShoppingCart = DonutShoppingCart@2cb6bc65
scala> donutShoppingCart1.add("Vanilla Donut")
DonutShoppingCart-> add method -> donutName: Vanilla Donut
res52: Long = 1
scala> donutShoppingCart1.update("Vanilla Donut")
DonutShoppingCart-> update method -> donutName: Vanilla Donut
res53: Boolean = true
scala> donutShoppingCart1.search("Vanilla Donut")
DonutShoppingCart-> search method -> donutName: Vanilla Donut
res54: String = Vanilla Donut
scala> donutShoppingCart1.delete("Vanilla Donut")
DonutShoppingCart-> delete method -> donutName: Vanilla Donut
res55: Boolean = true
scala> println("\nStep 4: Create an instance of DonutShoppingCart and assign its type to the trait DonutShoppingCartDao")
Step 4: Create an instance of DonutShoppingCart and assign its type to the trait DonutShoppingCartDao
scala> val donutShoppingCart2: DonutShoppingCartDao = new DonutShoppingCart()
donutShoppingCart2: DonutShoppingCartDao = DonutShoppingCart@1f24fb1
scala> donutShoppingCart2.add("Vanilla Donut")
DonutShoppingCart-> add method -> donutName: Vanilla Donut
res57: Long = 1
scala> donutShoppingCart2.update("Vanilla Donut")
DonutShoppingCart-> update method -> donutName: Vanilla Donut
res58: Boolean = true
scala> donutShoppingCart2.search("Vanilla Donut")
DonutShoppingCart-> search method -> donutName: Vanilla Donut
res59: String = Vanilla Donut
scala> donutShoppingCart2.delete("Vanilla Donut")
DonutShoppingCart-> delete method -> donutName: Vanilla Donut
res60: Boolean = true
val donutShoppingCart2: DonutShoppingCartDao[String] = new DonutShoppingCart[String]()
donutShoppingCart2.add("Vanilla Donut")
donutShoppingCart2.update("Vanilla Donut")
donutShoppingCart2.search("Vanilla Donut")
donutShoppingCart2.delete("Vanilla Donut")
scala> trait DonutShoppingCartDao[A] {
|
| def add(donut: A): Long
|
| def update(donut: A): Boolean
|
| def search(donut: A): A
|
| def delete(donut: A): Boolean
|
| }
defined trait DonutShoppingCartDao
scala> println("\nStep 2: Create a second trait which will define the methods for checking donut inventory")
Step 2: Create a second trait which will define the methods for checking donut inventory
scala> trait DonutInventoryService[A] {
|
| def checkStockQuantity(donut: A): Int
|
| }
defined trait DonutInventoryService
scala>
scala> println("\nStep 3: Create a DonutShoppingCart class which extends multiple traits namely trait DonutShoppingCartDao and trait DonutInventoryService")
Step 3: Create a DonutShoppingCart class which extends multiple traits namely trait DonutShoppingCartDao and trait DonutInventoryService
scala> class DonutShoppingCart[A] extends DonutShoppingCartDao[A] with DonutInventoryService[A] {
|
| override def add(donut: A): Long = {
| println(s"DonutShoppingCart-> add method -> donut: $donut")
| 1
| }
|
| override def update(donut: A): Boolean = {
| println(s"DonutShoppingCart-> update method -> donut: $donut")
| true
| }
|
| override def search(donut: A): A = {
| println(s"DonutShoppingCart-> search method -> donut: $donut")
| donut
| }
|
| override def delete(donut: A): Boolean = {
| println(s"DonutShoppingCart-> delete method -> donut: $donut")
| true
| }
|
| override def checkStockQuantity(donut: A): Int = {
| println(s"DonutShoppingCart-> checkStockQuantity method -> donut: $donut")
| 10
| }
| }
defined class DonutShoppingCart
scala> println("\nStep 4: Create an instance of DonutShoppingCart and call the add, update, search and delete methods")
Step 4: Create an instance of DonutShoppingCart and call the add, update, search and delete methods
scala> val donutShoppingCart: DonutShoppingCart[String] = new DonutShoppingCart[String]()
donutShoppingCart: DonutShoppingCart[String] = DonutShoppingCart@7bd395f7
scala> donutShoppingCart.add("Vanilla Donut")
DonutShoppingCart-> add method -> donut: Vanilla Donut
res73: Long = 1
scala> donutShoppingCart.update("Vanilla Donut")
DonutShoppingCart-> update method -> donut: Vanilla Donut
res74: Boolean = true
scala> donutShoppingCart.search("Vanilla Donut")
DonutShoppingCart-> search method -> donut: Vanilla Donut
res75: String = Vanilla Donut
scala> donutShoppingCart.delete("Vanilla Donut")
DonutShoppingCart-> delete method -> donut: Vanilla Donut
res76: Boolean = true
scala> println("\nStep 5: Call the checkStockQuantity method which was inherited from trait DonutInventoryService")
Step 5: Call the checkStockQuantity method which was inherited from trait DonutInventoryService
scala> donutShoppingCart.checkStockQuantity("Vanilla Donut")
DonutShoppingCart-> checkStockQuantity method -> donut: Vanilla Donut
res78: Int = 10
scala>
cala> trait DonutDatabase[A] {
|
| def addOrUpdate(donut: A): Long
|
| def query(donut: A): A
|
| def delete(donut: A): Boolean
| }
defined trait DonutDatabase
scala>
scala> println("\nStep 2: Create a class which extends trait DonutDatabase and knows how to perform CRUD operations with Apache Cassandra as storage layer")
Step 2: Create a class which extends trait DonutDatabase and knows how to perform CRUD operations with Apache Cassandra as storage layer
scala> class CassandraDonutStore[A] extends DonutDatabase[A] {
|
| override def addOrUpdate(donut: A): Long = {
| println(s"CassandraDonutDatabase-> addOrUpdate method -> donut: $donut")
| 1
| }
|
| override def query(donut: A): A = {
| println(s"CassandraDonutDatabase-> query method -> donut: $donut")
| donut
| }
|
| override def delete(donut: A): Boolean = {
| println(s"CassandraDonutDatabase-> delete method -> donut: $donut")
| true
| }
| }
defined class CassandraDonutStore
scala> println("\nStep 3: Create a trait which will define the methods for a data access layer and will require dependency injection for DonutDatabase
Step 3: Create a trait which will define the methods for a data access layer and will require dependency injection for DonutDatabase
scala> trait DonutShoppingCartDao[A] {
|
| val donutDatabase: DonutDatabase[A] // dependency injection
|
| def add(donut: A): Long = {
| println(s"DonutShoppingCartDao-> add method -> donut: $donut")
| donutDatabase.addOrUpdate(donut)
| }
|
| def update(donut: A): Boolean = {
| println(s"DonutShoppingCartDao-> update method -> donut: $donut")
| donutDatabase.addOrUpdate(donut)
| true
| }
|
| def search(donut: A): A = {
| println(s"DonutShoppingCartDao-> search method -> donut: $donut")
| donutDatabase.query(donut)
| }
|
| def delete(donut: A): Boolean = {
| println(s"DonutShoppingCartDao-> delete method -> donut: $donut")
| donutDatabase.delete(donut)
| }
|
| }
defined trait DonutShoppingCartDao
scala> println("\nStep 4: Create a trait which will define the methods for checking donut inventory and will require dependency injection for DonutDatabase")
Step 4: Create a trait which will define the methods for checking donut inventory and will require dependency injection for DonutDatabase
scala> trait DonutInventoryService[A] {
|
| val donutDatabase: DonutDatabase[A] // dependency injection
|
| def checkStockQuantity(donut: A): Int = {
| println(s"DonutInventoryService-> checkStockQuantity method -> donut: $donut")
| donutDatabase.query(donut)
| 1
| }
|
| }
defined trait DonutInventoryService
scala> println("\nStep 5: Create a trait which will act as a facade which extends multiple traits namely trait DonutShoppingCartDao and trait DonutInventoryService. It also inject the correct DonutDatabase implementation - a CassandraDonutStore")
Step 5: Create a trait which will act as a facade which extends multiple traits namely trait DonutShoppingCartDao and trait DonutInventoryService. It also inject the correct DonutDatabase implementation - a CassandraDonutStore
scala> trait DonutShoppingCartServices[A] extends DonutShoppingCartDao[A] with DonutInventoryService[A] {
| override val donutDatabase: DonutDatabase[A] = new CassandraDonutStore[A]()
| }
defined trait DonutShoppingCartServices
scala> println("\nStep 6: Create a DonutShoppingCart class which extends a single facade named DonutShoppingCartServices to expose all the underlying features required by a DonutShoppingCart")
Step 6: Create a DonutShoppingCart class which extends a single facade named DonutShoppingCartServices to expose all the underlying features required by a DonutShoppingCart
scala> class DonutShoppingCart[A] extends DonutShoppingCartServices[A] {
|
| }
defined class DonutShoppingCart
scala> println("\nStep 7: Create an instance of DonutShoppingCart and call the add, update, search and delete methods")
Step 7: Create an instance of DonutShoppingCart and call the add, update, search and delete methods
scala> val donutShoppingCart: DonutShoppingCart[String] = new DonutShoppingCart[String]()
donutShoppingCart: DonutShoppingCart[String] = DonutShoppingCart@5a4be77f
scala> donutShoppingCart.add("Vanilla Donut")
DonutShoppingCartDao-> add method -> donut: Vanilla Donut
CassandraDonutDatabase-> addOrUpdate method -> donut: Vanilla Donut
res85: Long = 1
scala> donutShoppingCart.update("Vanilla Donut")
DonutShoppingCartDao-> update method -> donut: Vanilla Donut
CassandraDonutDatabase-> addOrUpdate method -> donut: Vanilla Donut
res86: Boolean = true
scala> donutShoppingCart.search("Vanilla Donut")
DonutShoppingCartDao-> search method -> donut: Vanilla Donut
CassandraDonutDatabase-> query method -> donut: Vanilla Donut
res87: String = Vanilla Donut
scala> donutShoppingCart.delete("Vanilla Donut")
DonutShoppingCartDao-> delete method -> donut: Vanilla Donut
CassandraDonutDatabase-> delete
method -> donut: Vanilla Donut
res88: Boolean = true
scala> donutShoppingCart.checkStockQuantity("Vanilla Donut")
DonutInventoryService-> checkStockQuantity method -> donut: Vanilla Donut
CassandraDonutDatabase-> query method -> donut: Vanilla Donut
res89: Int = 1
scala>
cala> println("Step 1: Create a trait which will define the methods for a data access layer")
Step 1: Create a trait which will define the methods for a data access layer
scala> trait DonutShoppingCartDao {
|
| def add(donutName: String): Long
|
| def update(donutName: String): Boolean
|
| def search(donutName: String): String
|
| def delete(donutName: String): Boolean
|
| }
defined trait DonutShoppingCartDao
scala> println("\nStep 2: Create a DonutShoppingCart class which extends the trait from Step 1 and implements its methods")
Step 2: Create a DonutShoppingCart class which extends the trait from Step 1 and implements its methods
scala> class DonutShoppingCart extends DonutShoppingCartDao {
|
| override def add(donutName: String): Long = {
| println(s"DonutShoppingCart-> add method -> donutName: $donutName")
| 1
| }
|
| override def update(donutName: String): Boolean = {
| println(s"DonutShoppingCart-> update method -> donutName: $donutName")
| true
| }
|
| override def search(donutName: String): String = {
| println(s"DonutShoppingCart-> search method -> donutName: $donutName")
| donutName
| }
|
| override def delete(donutName: String): Boolean = {
| println(s"DonutShoppingCart-> delete method -> donutName: $donutName")
| true
| }
| }
defined class DonutShoppingCart
scala>
scala> println("\nStep 3: Create an instance of DonutShoppingCart and call the add, update, search and delete methods")
Step 3: Create an instance of DonutShoppingCart and call the add, update, search and delete methods
scala> val donutShoppingCart1: DonutShoppingCart = new DonutShoppingCart()
donutShoppingCart1: DonutShoppingCart = DonutShoppingCart@2cb6bc65
scala> donutShoppingCart1.add("Vanilla Donut")
DonutShoppingCart-> add method -> donutName: Vanilla Donut
res52: Long = 1
scala> donutShoppingCart1.update("Vanilla Donut")
DonutShoppingCart-> update method -> donutName: Vanilla Donut
res53: Boolean = true
scala> donutShoppingCart1.search("Vanilla Donut")
DonutShoppingCart-> search method -> donutName: Vanilla Donut
res54: String = Vanilla Donut
scala> donutShoppingCart1.delete("Vanilla Donut")
DonutShoppingCart-> delete method -> donutName: Vanilla Donut
res55: Boolean = true
scala> println("\nStep 4: Create an instance of DonutShoppingCart and assign its type to the trait DonutShoppingCartDao")
Step 4: Create an instance of DonutShoppingCart and assign its type to the trait DonutShoppingCartDao
scala> val donutShoppingCart2: DonutShoppingCartDao = new DonutShoppingCart()
donutShoppingCart2: DonutShoppingCartDao = DonutShoppingCart@1f24fb1
scala> donutShoppingCart2.add("Vanilla Donut")
DonutShoppingCart-> add method -> donutName: Vanilla Donut
res57: Long = 1
scala> donutShoppingCart2.update("Vanilla Donut")
DonutShoppingCart-> update method -> donutName: Vanilla Donut
res58: Boolean = true
scala> donutShoppingCart2.search("Vanilla Donut")
DonutShoppingCart-> search method -> donutName: Vanilla Donut
res59: String = Vanilla Donut
scala> donutShoppingCart2.delete("Vanilla Donut")
DonutShoppingCart-> delete method -> donutName: Vanilla Donut
res60: Boolean = true
val donutShoppingCart2: DonutShoppingCartDao[String] = new DonutShoppingCart[String]()
donutShoppingCart2.add("Vanilla Donut")
donutShoppingCart2.update("Vanilla Donut")
donutShoppingCart2.search("Vanilla Donut")
donutShoppingCart2.delete("Vanilla Donut")
scala> trait DonutShoppingCartDao[A] {
|
| def add(donut: A): Long
|
| def update(donut: A): Boolean
|
| def search(donut: A): A
|
| def delete(donut: A): Boolean
|
| }
defined trait DonutShoppingCartDao
scala> println("\nStep 2: Create a second trait which will define the methods for checking donut inventory")
Step 2: Create a second trait which will define the methods for checking donut inventory
scala> trait DonutInventoryService[A] {
|
| def checkStockQuantity(donut: A): Int
|
| }
defined trait DonutInventoryService
scala>
scala> println("\nStep 3: Create a DonutShoppingCart class which extends multiple traits namely trait DonutShoppingCartDao and trait DonutInventoryService")
Step 3: Create a DonutShoppingCart class which extends multiple traits namely trait DonutShoppingCartDao and trait DonutInventoryService
scala> class DonutShoppingCart[A] extends DonutShoppingCartDao[A] with DonutInventoryService[A] {
|
| override def add(donut: A): Long = {
| println(s"DonutShoppingCart-> add method -> donut: $donut")
| 1
| }
|
| override def update(donut: A): Boolean = {
| println(s"DonutShoppingCart-> update method -> donut: $donut")
| true
| }
|
| override def search(donut: A): A = {
| println(s"DonutShoppingCart-> search method -> donut: $donut")
| donut
| }
|
| override def delete(donut: A): Boolean = {
| println(s"DonutShoppingCart-> delete method -> donut: $donut")
| true
| }
|
| override def checkStockQuantity(donut: A): Int = {
| println(s"DonutShoppingCart-> checkStockQuantity method -> donut: $donut")
| 10
| }
| }
defined class DonutShoppingCart
scala> println("\nStep 4: Create an instance of DonutShoppingCart and call the add, update, search and delete methods")
Step 4: Create an instance of DonutShoppingCart and call the add, update, search and delete methods
scala> val donutShoppingCart: DonutShoppingCart[String] = new DonutShoppingCart[String]()
donutShoppingCart: DonutShoppingCart[String] = DonutShoppingCart@7bd395f7
scala> donutShoppingCart.add("Vanilla Donut")
DonutShoppingCart-> add method -> donut: Vanilla Donut
res73: Long = 1
scala> donutShoppingCart.update("Vanilla Donut")
DonutShoppingCart-> update method -> donut: Vanilla Donut
res74: Boolean = true
scala> donutShoppingCart.search("Vanilla Donut")
DonutShoppingCart-> search method -> donut: Vanilla Donut
res75: String = Vanilla Donut
scala> donutShoppingCart.delete("Vanilla Donut")
DonutShoppingCart-> delete method -> donut: Vanilla Donut
res76: Boolean = true
scala> println("\nStep 5: Call the checkStockQuantity method which was inherited from trait DonutInventoryService")
Step 5: Call the checkStockQuantity method which was inherited from trait DonutInventoryService
scala> donutShoppingCart.checkStockQuantity("Vanilla Donut")
DonutShoppingCart-> checkStockQuantity method -> donut: Vanilla Donut
res78: Int = 10
scala>
cala> trait DonutDatabase[A] {
|
| def addOrUpdate(donut: A): Long
|
| def query(donut: A): A
|
| def delete(donut: A): Boolean
| }
defined trait DonutDatabase
scala>
scala> println("\nStep 2: Create a class which extends trait DonutDatabase and knows how to perform CRUD operations with Apache Cassandra as storage layer")
Step 2: Create a class which extends trait DonutDatabase and knows how to perform CRUD operations with Apache Cassandra as storage layer
scala> class CassandraDonutStore[A] extends DonutDatabase[A] {
|
| override def addOrUpdate(donut: A): Long = {
| println(s"CassandraDonutDatabase-> addOrUpdate method -> donut: $donut")
| 1
| }
|
| override def query(donut: A): A = {
| println(s"CassandraDonutDatabase-> query method -> donut: $donut")
| donut
| }
|
| override def delete(donut: A): Boolean = {
| println(s"CassandraDonutDatabase-> delete method -> donut: $donut")
| true
| }
| }
defined class CassandraDonutStore
scala> println("\nStep 3: Create a trait which will define the methods for a data access layer and will require dependency injection for DonutDatabase
Step 3: Create a trait which will define the methods for a data access layer and will require dependency injection for DonutDatabase
scala> trait DonutShoppingCartDao[A] {
|
| val donutDatabase: DonutDatabase[A] // dependency injection
|
| def add(donut: A): Long = {
| println(s"DonutShoppingCartDao-> add method -> donut: $donut")
| donutDatabase.addOrUpdate(donut)
| }
|
| def update(donut: A): Boolean = {
| println(s"DonutShoppingCartDao-> update method -> donut: $donut")
| donutDatabase.addOrUpdate(donut)
| true
| }
|
| def search(donut: A): A = {
| println(s"DonutShoppingCartDao-> search method -> donut: $donut")
| donutDatabase.query(donut)
| }
|
| def delete(donut: A): Boolean = {
| println(s"DonutShoppingCartDao-> delete method -> donut: $donut")
| donutDatabase.delete(donut)
| }
|
| }
defined trait DonutShoppingCartDao
scala> println("\nStep 4: Create a trait which will define the methods for checking donut inventory and will require dependency injection for DonutDatabase")
Step 4: Create a trait which will define the methods for checking donut inventory and will require dependency injection for DonutDatabase
scala> trait DonutInventoryService[A] {
|
| val donutDatabase: DonutDatabase[A] // dependency injection
|
| def checkStockQuantity(donut: A): Int = {
| println(s"DonutInventoryService-> checkStockQuantity method -> donut: $donut")
| donutDatabase.query(donut)
| 1
| }
|
| }
defined trait DonutInventoryService
scala> println("\nStep 5: Create a trait which will act as a facade which extends multiple traits namely trait DonutShoppingCartDao and trait DonutInventoryService. It also inject the correct DonutDatabase implementation - a CassandraDonutStore")
Step 5: Create a trait which will act as a facade which extends multiple traits namely trait DonutShoppingCartDao and trait DonutInventoryService. It also inject the correct DonutDatabase implementation - a CassandraDonutStore
scala> trait DonutShoppingCartServices[A] extends DonutShoppingCartDao[A] with DonutInventoryService[A] {
| override val donutDatabase: DonutDatabase[A] = new CassandraDonutStore[A]()
| }
defined trait DonutShoppingCartServices
scala> println("\nStep 6: Create a DonutShoppingCart class which extends a single facade named DonutShoppingCartServices to expose all the underlying features required by a DonutShoppingCart")
Step 6: Create a DonutShoppingCart class which extends a single facade named DonutShoppingCartServices to expose all the underlying features required by a DonutShoppingCart
scala> class DonutShoppingCart[A] extends DonutShoppingCartServices[A] {
|
| }
defined class DonutShoppingCart
scala> println("\nStep 7: Create an instance of DonutShoppingCart and call the add, update, search and delete methods")
Step 7: Create an instance of DonutShoppingCart and call the add, update, search and delete methods
scala> val donutShoppingCart: DonutShoppingCart[String] = new DonutShoppingCart[String]()
donutShoppingCart: DonutShoppingCart[String] = DonutShoppingCart@5a4be77f
scala> donutShoppingCart.add("Vanilla Donut")
DonutShoppingCartDao-> add method -> donut: Vanilla Donut
CassandraDonutDatabase-> addOrUpdate method -> donut: Vanilla Donut
res85: Long = 1
scala> donutShoppingCart.update("Vanilla Donut")
DonutShoppingCartDao-> update method -> donut: Vanilla Donut
CassandraDonutDatabase-> addOrUpdate method -> donut: Vanilla Donut
res86: Boolean = true
scala> donutShoppingCart.search("Vanilla Donut")
DonutShoppingCartDao-> search method -> donut: Vanilla Donut
CassandraDonutDatabase-> query method -> donut: Vanilla Donut
res87: String = Vanilla Donut
scala> donutShoppingCart.delete("Vanilla Donut")
DonutShoppingCartDao-> delete method -> donut: Vanilla Donut
CassandraDonutDatabase-> delete
method -> donut: Vanilla Donut
res88: Boolean = true
scala> donutShoppingCart.checkStockQuantity("Vanilla Donut")
DonutInventoryService-> checkStockQuantity method -> donut: Vanilla Donut
CassandraDonutDatabase-> query method -> donut: Vanilla Donut
res89: Int = 1
scala>
No comments:
Post a Comment