println("Step 1: Create a trait which knows how to do create, read, update and delete operations CRUD to a given database")
trait DonutDatabase[A] { def addOrUpdate(donut: A): Long def query(donut: A): A def delete(donut: A): Boolean
} println("\nStep 2: Create a class which extends trait DonutDatabase and knows how to perform CRUD operations with Apache Cassandra as storage layer")
class CassandraDonutStore[A] extends DonutDatabase[A] { override def addOrUpdate(donut: A): Long = {
println(s"CassandraDonutDatabase-> addOrUpdate method -> donut: $donut") } 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
}
} println("\nStep 3: Create a trait which will define the methods for a data access layer and will require dependency injection for DonutDatabase")
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)
} } println("\nStep 4: Create a trait which will define the methods for checking donut inventory and will require dependency injection for DonutDatabase")
trait DonutInventoryService[A] { val donutDatabase: DonutDatabase[A] // dependency injection def checkStockQuantity(donut: A): Int = {
println(s"DonutInventoryService-> checkStockQuantity method -> donut: $donut")
donutDatabase.query(donut) } } 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")
trait DonutShoppingCartServices[A] extends DonutShoppingCartDao[A] with DonutInventoryService[A] {
override val donutDatabase: DonutDatabase[A] = new CassandraDonutStore[A]()
} println("\nStep 6: Create a DonutShoppingCart class which extends a single facade named DonutShoppingCartServices to expose all the underlying features required by a DonutShoppingCart")
class DonutShoppingCart[A] extends DonutShoppingCartServices[A] { } println("\nStep 7: Create an instance of DonutShoppingCart and call the add, update, search and delete methods")
val donutShoppingCart: DonutShoppingCart[String] = new DonutShoppingCart[String]()
donutShoppingCart.add("Vanilla Donut")
donutShoppingCart.update("Vanilla Donut")
donutShoppingCart.search("Vanilla Donut")
donutShoppingCart.delete("Vanilla Donut") println("\nStep 8: Call the checkStockQuantity method")
donutShoppingCart.checkStockQuantity("Vanilla Donut")

result:

Step 1: Create a trait which knows how to do create, read, update and delete operations CRUD to a given database

Step 2: Create a class which extends trait DonutDatabase and knows how to perform CRUD operations with Apache Cassandra as storage layer

Step 3: Create a trait which will define the methods for a data access layer 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

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

Step 6: Create a DonutShoppingCart class which extends a single facade named DonutShoppingCartServices to expose all the underlying features required by a DonutShoppingCart

Step 7: Create an instance of DonutShoppingCart and call the add, update, search and delete methods
DonutShoppingCartDao-> add method -> donut: Vanilla Donut
CassandraDonutDatabase-> addOrUpdate method -> donut: Vanilla Donut
DonutShoppingCartDao-> update method -> donut: Vanilla Donut
CassandraDonutDatabase-> addOrUpdate method -> donut: Vanilla Donut
DonutShoppingCartDao-> search method -> donut: Vanilla Donut
CassandraDonutDatabase-> query method -> donut: Vanilla Donut
DonutShoppingCartDao-> delete method -> donut: Vanilla Donut
CassandraDonutDatabase-> delete method -> donut: Vanilla Donut Step 8: Call the checkStockQuantity method
DonutInventoryService-> checkStockQuantity method -> donut: Vanilla Donut
CassandraDonutDatabase-> query method -> donut: Vanilla Donut

  

learning scala dependency injection的更多相关文章

  1. Scala 深入浅出实战经典 第57讲:Scala中Dependency Injection实战详解

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  2. [LINK]List of .NET Dependency Injection Containers (IOC)

    http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx I'm trying to expand my ...

  3. Ninject学习(一) - Dependency Injection By Hand

    大体上是把官网上的翻译下而已. http://www.ninject.90iogjkdcrorg/wiki.html Dependency Injection By Hand So what's Ni ...

  4. MVC Controller Dependency Injection for Beginners【翻译】

    在codeproject看到一篇文章,群里的一个朋友要帮忙我翻译一下顺便贴出来,这篇文章适合新手,也算是对MEF的一个简单用法的介绍. Introduction In a simple stateme ...

  5. 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)

    控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...

  6. [转载][翻译] IoC 容器和 Dependency Injection 模式

    原文地址:Inversion of Control Containers and the Dependency Injection pattern 中文翻译版本是网上的PDF文档,发布在这里仅为方便查 ...

  7. Dependency Injection

    Inversion of Control - Dependency Injection - Dependency Lookup loose coupling/maintainability/ late ...

  8. Inversion of Control Containers and the Dependency Injection pattern(转)

    In the Java community there's been a rush of lightweight containers that help to assemble components ...

  9. 【译】Dependency Injection with Autofac

    先说下为什么翻译这篇文章,既定的方向是架构,然后为了学习架构就去学习一些架构模式.设计思想. 突然有一天发现依赖注入这种技能.为了使得架构可测试.易维护.可扩展,需要架构设计为松耦合类型,简单的说也就 ...

随机推荐

  1. Android Manifest 中 uses-feature 和 uses-permission的作用 关系和区别

    Manifest中的 <uses-permission android:name="android.permission.CAMERA" /> 和 <uses-f ...

  2. 使用脚本将AspNetCore发布到IIS上

    首先你必须要了解的是,没有脚本的情况下,如何把AspNetCore的应用发布到IIS上. 大致分为这些步骤: 安装MS C++ 2015 x86&x64 安装正确版本的.NET Core Ru ...

  3. Spring Boot集成redis完整实例

    添加依赖: (Spring Data Redis) 启动redis: 配置文件中进行配置: redis基本使用思路: redis中不存在就查询数据库然后存入redis: 查看日志:

  4. 最简容器化动手小实践——再战flappybird

    <Flappy Bird>是一名越南开发者所开发的游戏,这款游戏的主要内容是帮助一只小鸟穿越水管的层层阻碍,玩家所需要的只是点击屏幕从而调整小鸟的高度.而令这款游戏与众不同的是,这款游戏的 ...

  5. shiro与spring集成

    简介 Apache Shiro 是 Java 的一个安全(权限)框架.主要提供了认证.授权.加密和会话管理等功能. Authentication:身份认证/登录,验证用户是不是拥有相应的身份:Auth ...

  6. CentOS7 安装 Git

    环境: 系统版本:CentOS 7.5 Git 版本:2.20.1 一.安装 Git 1.下载编译工具 $ yum -y groupinstall "Development Tools&qu ...

  7. 天梯赛 L2-024. 部落

    题解:并查集,这里要用路径压缩来优化 代码:// 这里范围理错了, 浪费20分钟debug #include <set> #include <iostream> #includ ...

  8. 【转载】Spring JdbcTemplate详解

    JdbcTemplate简介 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中. JdbcTempla ...

  9. Spark机器学习基础-无监督学习

    0.K-means from __future__ import print_function from pyspark.ml.clustering import KMeans#硬聚类 #from p ...

  10. Java 之 反射机制

    反射:框架设计的灵魂 框架:是一个可以供我们使用的半成品软件.可以在框架的基础上进行软件开发,简化编码. 反射:将类的各个组成部分封装为其他对象,这就是反射机制. 好处: 1. 可以在程序运行过程中, ...