Q
设计模式和斯卡拉
9
A
回答
6
类似这样的清单已经被整理过了。见https://wiki.scala-lang.org/display/SYGN/Design+Patterns
7
让我们先从“Singleton模式”:
object SomeSingleton //That's it
我会还提出“使用 - 功能 - 的 - 高阶模式”。 而不是e。 G。通过自己迭代集合,您可以为类提供的方法提供函数。
Scala里,你基本上说,你打算做什么:
//declare some example class
case class Person(name: String, age: Int)
//create some example persons
val persons = List(Person("Joe", 42), Person("Jane", 30), Person("Alice", 14), Person("Bob", 12))
//"Are there any persons in this List, which are older than 18?"
persons.exists(_.age > 18)
// => Boolean = true
//"Is every person's name longer than 4 characters?"
persons.forall(_.name.length > 4)
// => Boolean = false
//"I need a List of only the adult persons!"
persons.filter(_.age >= 18)
// => List[Person] = List(Person(Joe,42), Person(Jane,30))
//"Actually I need both, a list with the adults and a list of the minors!"
persons.partition(_.age >= 18)
// => (List[Person], List[Person]) = (List(Person(Joe,42), Person(Jane,30)),List(Person(Alice,14), Person(Bob,12)))
//"A List with the names, please!"
persons.map(_.name)
// => List[String] = List(Joe, Jane, Alice, Bob)
//"I would like to know how old all persons are all together!"
persons.foldLeft(0)(_ + _.age)
// => Int = 98
在Java中这样做将意味着触摸收集自己的元素和混合与流量控制代码的应用程序逻辑。
More information关于Collection类。
这个漂亮EPFL paper有关弃用Observer模式可能会感兴趣了。
Typeclasses是构建在哪里继承并不真正适合类常用功能的一种方法。
1
相关问题
- 1. 模式在斯卡拉
- 2. 斯卡拉工厂模式
- 3. 斯卡拉模式匹配
- 4. case类,模式匹配和斯卡拉
- 5. 斯卡拉策略模式设计问题
- 6. 斯卡拉工厂模式提高了设计
- 7. 斯卡拉和SBT设置
- 8. 斯卡拉功能API设计
- 9. 斯卡拉模块
- 10. 斯卡拉:隐式
- 11. 斯卡拉模板中设置变量
- 12. 斯卡拉子类模式匹配
- 13. 斯卡拉填充模板的方式?
- 14. 斯卡拉模式有或无结合
- 15. 斯卡拉鸭打字模式匹配
- 16. 模式匹配或isInstanceOf斯卡拉
- 17. 模式匹配 - 火花斯卡拉RDD
- 18. 斯卡拉如何通过模式
- 19. 模式匹配穿过斯卡拉
- 20. 使用模式在斯卡拉
- 21. 模式匹配的斯卡拉`class`
- 22. PySpark和斯卡拉
- 23. &&和||在斯卡拉
- 24. 斯卡拉计数变量
- 25. 文档计数/斯卡拉
- 26. 关于分布式系统设计的问题 - 斯卡拉
- 27. 斯卡拉重现设置和日志
- 28. 将数据转换为多种类型之一的设计模式[斯卡拉]
- 29. 斯卡拉设置功能
- 30. 斯卡拉误差建模
意识到它太晚了,但这真的应该是社区wiki – 2010-10-22 02:47:31
@Dave同意,我不认为这是一个SO法律问题。但是,我很感兴趣看到答案,我希望它继续! – JAL 2010-10-22 04:47:19
您可能还想链接到[此问题](http://stackoverflow.com/questions/5566708/design-patterns-for-static-type-checking)。 – ziggystar 2011-05-05 07:30:34