14

I'm writing this question to maintain a register of design patterns associated with Scala, standards patterns or only from this language.

Associated questions:

Thanks to all who contribute

Community
  • 1
  • 1
adelarsq
  • 3,718
  • 4
  • 37
  • 47

4 Answers4

7

Let's start with the "Singleton pattern":

object SomeSingleton //That's it

I would additionally propose the "Using-functions-of-higher-order pattern". Instead of e. g. iterating through a collection by yourself you supply functions to the methods the classes provide.

In Scala you basically say what you intend to do:

//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

Doing this in Java would have meant touching the elements of a collection yourself and mix your application logic with flow control code.

More information about the Collection classes.


This nice EPFL paper about Deprecating the Observer Pattern might be of interest, too.


Typeclasses are one approach to structure common features of classes where inheritance doesn't really fit.

soc
  • 27,983
  • 20
  • 111
  • 215
  • 2
    It's sad that the "level" or common programming language is such that these ... constructs ... must be brought out and called "design patterns" (as if they should warrant any additional burden :-) –  Oct 22 '10 at 00:45
  • 2
    "Using-functions-of-higher-order pattern" is GoF's Strategy – Synesso Oct 22 '10 at 00:57
  • To make singletons more testable it is always better to leave partial if not full implementation in a trait. //code trait SomeSingleton{ def doSomething1{} lazy val val1 } object SomeSingleton extends SomeSingleton – Nick Apr 21 '14 at 13:53
6

A list like this has been collated already. See https://wiki.scala-lang.org/display/SYGN/Design+Patterns

Daenyth
  • 35,856
  • 13
  • 85
  • 124
Synesso
  • 37,610
  • 35
  • 136
  • 207
1

This is a list of materials related to design patterns using Scala language:

adelarsq
  • 3,718
  • 4
  • 37
  • 47
-5
//Triangle pattern using Scala:
object Exercise {
  def main(args:Array [String]){
    var n = 6
   for (i<-1 to 6){

     for (j<-i to (2*n-2))
     {
       print(" ")       
     }
     for(j<- 0 to i-1)
     {
     print(" *")
   }
     println(" ")
   }


  }
}

Output: * * * * * * * * * * * * * * * * * * * * *

  • 1
    This answer has nothing to do with design patterns and is unrelated to the question. – jwvh Apr 19 '20 at 17:17
  • Design Patterns are typical solutions to commonly occurring problems in software design. This answer is unrelated. – Sangeeta Nov 30 '20 at 17:09