加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – Cake Pattern可以用于非单例样式依赖吗?

发布时间:2020-12-16 09:52:21 所属栏目:安全 来源:网络整理
导读:我遇到的蛋糕模式的大多数示例似乎都将依赖关系视为单例类型服务;在组件的最终组装中,每种类型只有一个实例.在使用Cake Pattern进行依赖注入时,是否可以编写具有多个特定类型实例的配置(可能以不同方式配置)? 请考虑以下组件.通用HTTP服务: trait HttpServ
我遇到的蛋糕模式的大多数示例似乎都将依赖关系视为单例类型服务;在组件的最终组装中,每种类型只有一个实例.在使用Cake Pattern进行依赖注入时,是否可以编写具有多个特定类型实例的配置(可能以不同方式配置)?

请考虑以下组件.通用HTTP服务:

trait HttpService { def get(query:String):String }
trait HttpServiceComponent {
  val httpService:HttpService
  class HttpServiceImpl(address:String) extends HttpService {
    def get(query:String):String = ...
  }
}

贸易与贸易公司服务,每个服务都依赖于HttpService,它可能是不同的实例:

trait TradeService { def lastTrade(symbol:String):String }
trait TradeServiceComponent {
  this:HttpServiceComponent => // Depends on HttpService
  val tradeService:TradeService
  class TradeServiceImpl extends TradeService {
    def lastTrade(symbol:String):String =
      httpService.get("symbol=" + symbol)
  }
}

trait CompanyService { def getCompanySymbols(exchange:String):String }
trait CompanyServiceComponent {
  this:HttpServiceComponent =>  // Depends on different HttpService instance
  val companyService:CompanyService
  class CompanyServiceImpl extends CompanyService {
    def getCompanySymbols(exchange:String):String =
      httpService.get("exchange=" + exchange)
  }
}

主要应用程序组件依赖于Trade&公司服务:

trait App { def run(exchange:String):Unit }
trait AppComponent {
  this:CompanyServiceComponent with TradeServiceComponent =>
  val app:App
  class AppImpl extends App {
    def run(exchange:String) =
      companyService.getCompanySymbols(exchange).split(",").foreach(sym => {
        val lastTrade = tradeService.lastTrade(sym)
        printf("Last trade for %s: %s".format(sym,lastTrade))
      })
  }
}

是否可以连接App以使其TradeService使用指向一个地址的HttpService,并且其CompanySerivce使用指向另一个地址的不同HttpService实例?

解决方法

正如你从答案中看到的那样(尤其是丹尼尔的,也是你自己的),这是可能的,但它看起来并不优雅.出现这种困难是因为当您使用Cake模式时,将所有必需的特征混合到一个对象中(使用“with”关键字),并且您不能将特征多次混合到一个实例中.这就是mixins的工作方式,而Cake也是基于它们的.

你可以强制Cake处理非单例依赖的事实并不意味着你应该这样做.我建议你在这种情况下简单地使用普通的构造函数,这就是自我类型注释不适合的地方:

trait HttpService { ... }

/* HttpServiceImpl has become a top-level class now,* as the Cake pattern adds no more value here.
 * In addition,trait HttpServiceComponent gets deleted */
class HttpServiceImpl(address:String) extends HttpService {
  ...
}

trait TradeService { def lastTrade(symbol:String):String }
trait TradeServiceComponent {
  // The dependency on HttpService is no longer declared as self-type
  val tradeService:TradeService
  // It is declared as a constructor parameter now
  class TradeServiceImpl(httpService: HttpService) extends TradeService {
    def lastTrade(symbol:String):String =
      httpService.get("symbol=" + symbol)
  }
}

trait CompanyService { def getCompanySymbols(exchange:String):String }
trait CompanyServiceComponent {
  // Again,self-type annotation deleted
  val companyService:CompanyService
  // Again,the dependency is declared as a constructor parameter
  class CompanyServiceImpl(httpService: HttpService) extends CompanyService {
    def getCompanySymbols(exchange:String):String =
      httpService.get("exchange=" + exchange)
  }
}

App和AppComponent特征保持其原始形式.现在,您可以通过以下方式使用所有组件:

object App {
  def main(args:Array[String]):Unit = {
    val appAssembly = new AppComponent 
        with TradeServiceComponent
        with CompanyServiceComponent {
      // Note,that HttpServiceComponent it neither needed nor mixed-in now
      val tradeService = new TradeServiceImpl(
        new HttpServiceImpl("http://trades-r-us.com"))
      val companyService = new CompanyServiceImpl(
        new HttpServiceImpl("http://exchange-services.com"))
      val app = new AppImpl
    }
    appAssembly.app.run(args(0))
  }
}

此外,您可能需要仔细检查Cake模式是否真的最适合您的需求,因为它实际上是一个复杂的模式,依赖注入只是其中的一部分.如果你只将它用于DI,我会建议你使用更简单的解决方案.我发表了关于here的博文.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读