蛋糕模式在scala中的重要性 [英] importance of cake pattern in scala

查看:122
本文介绍了蛋糕模式在scala中的重要性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习scala一段时间了,现在正在研究蛋糕模式.我从此处

I have started learning scala for a while now and now looking at cake pattern. I got the example from here

trait UserRepositoryComponent {
  def userLocator: UserLocator

  trait UserLocator {
    def findAll: List[User]
  }
}

trait UserRepositoryJPAComponent extends UserRepositoryComponent {
  val em: EntityManager

  def userLocator = new UserLocatorJPA(em)

  class UserLocatorJPA(val em: EntityManager) extends UserLocator {
    def findAll = {
      println("Executing a JPA query")
      List(new User, new User)
    }
  }
}

trait UserServiceComponent {
  def userService: UserService

  trait UserService {
    def findAll: List[User]
  }
}

trait DefaultUserServiceComponent extends UserServiceComponent {
  this: UserRepositoryComponent =>

  def userService = new DefaultUserService

  class DefaultUserService extends UserService {
    def findAll = userLocator.findAll
  }
}

在我看来,太多样板代码无法将JPA存储库注入服务.

To me it looks like too many boilerplate code to get the JPA repository injected to service.

但是此代码将以更少的行数执行相同的操作

However this code would do the same with much lesser number of lines

trait UserRepository {
  def findAll
}

trait JPAUserRepository extends UserRepository {
  val em: EntityManager
  def findAll = {
    em.createQuery
    println("find using JPA")
  }
}

trait MyService {
  def findAll
}

trait MyDefaultService extends MyService {
  this: UserRepository=>
}

实例化这两种情况.

val t1 = new DefaultUserServiceComponent with UserRepositoryJPAComponent {
  val em = new EntityManager()
}
t1.userService.findAll


val t2 = new MyDefaultService with JPAUserRepository {
  val em = new EntityManager
}

t2.findAll

第二种情况使用更少的代码,并使用DI.您能帮我了解蛋糕图案带来的额外好处吗?

Second scenario uses much less code, and uses DI. Can you help me understand what extra advantages cake pattern brings.

推荐答案

据我了解,没有太大区别.实际上,蛋糕图案为IoC.这只是实现IoCDI的想法,无需单独的DI框架,而仅使用scala代码.除非您需要更多功能,否则您可能应该优先使用它而不是单独的DI容器.

As I understand it, there is no much difference. Actually cake pattern is IoC. It's just the idea of implementing IoC and DI, without separate DI framework, but just with scala code. You probably should prefer it over separate DI container unless you need more functionality.

在我看来,您的两个示例都是蛋糕模式.至少我是这样理解的.但是Martin在他的书中并未将其命名为蛋糕图案",而且我将scala mosly的知识基于一本书,因此我可能会遗漏一些东西.我的理解是蛋糕模式是结合不同特征以实现DI

Also it looks to me like both of your examples are cake patterns. At least that's how I understand it. But Martin didn't name it "cake pattern" in his book, and I base my knowledge of scala mosly on a single book, so I might be missing something. My understanding is that cake pattern is idea combining different traits to achieve DI

我认为Martin在他的书中特别提到过,可以在scala中使用诸如Spring之类的DI容器,但是不幸的是我找不到这个地方

I think Martin specifically mentioned in his book, that it is alright to use DI-containers such as Spring in scala, but I unfortunately cannot find this place

更新

找到它: http://www.artima.com/pins1ed/modular-programming-using-objects.html 参见27.1 The problem的最后一个小节.但是正如我说的那样,他在这里不是在谈论蛋糕",尽管从您给我的文章看,想法似乎是一样的

Found it: http://www.artima.com/pins1ed/modular-programming-using-objects.html See last subparagraph of 27.1 The problem. But as I said, he is not talking about "cakes" here, though the idea looks the same from the article you gave

更新2

我只是重新阅读了我的答案,并了解到我需要改进它,因为它不能完全回答问题.

I've just reread my answer and understood that I need to improve it, as it does not fully answers the question.

您应该更喜欢蛋糕模式",因为它更简单.如果您使用Spring,则必须维护配置(无论是XML还是注释),您可能还对类有一些要求(我没有使用过Spring,所以不确定是否有任何要求),并且您有带上整个春天.使用蛋糕模式,您只需编写简单的代码即可(您的第二个示例很简单,您应该同意). scala的优点是您可以使用它做很多事情,并且仅使用一些框架(如果将其与java进行比较),通常会使用更多的外部库

You should prefer "cake pattern", because it is simpler. If you use Spring, you have to maintain configuration, whether it is XML or annotations, you may also have some requirements over your classes (I haven't used Spring, so I'm not sure whether there are any), and you have to bring whole Spring with you. With cake pattern you just write code as simple as it is (your second example is simple, you should agree). What's nice about scala is that you can do a lot of stuff with it, and use only a few frameworks - if you compare it to java, - you usually use many more external libraries

如果您需要代理等更高级的功能-您可以切换到Spring或继续使用Scala并解决语言本身的问题,希望scala非常强大,甚至可以涵盖复杂的情况.

If you ever need more advanced functionality, like proxies - you may switch to Spring, or continue use Scala and solve your problems with the language itself, hopefully scala is extremely powerful and should cover even complicated cases.

您提供的两个代码段之间的区别只是抽象:第一个对存储库和服务中定义的操作进行了另一个抽象,而这些都不是模式的一部分.我不认为这是必需的,但作者决定这样显示.

The difference between two code pieces you provided is just abstraction: the first one has one more abstraction over operations defined in repository and service, and these are not part of the pattern. I do not feel like this is required, but author decided to show it like this.

这篇关于蛋糕模式在scala中的重要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆