Skip to content

Commit a97719b

Browse files
committed
Add class composition with mixins examples
1 parent 4b044a5 commit a97719b

File tree

2 files changed

+119
-0
lines changed
  • core-scala/src

2 files changed

+119
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.scala.classcompositionwithtraits
2+
3+
import com.baeldung.scala.classcompositionwithtraits.CarTraits.{Car, Printable}
4+
5+
object CarTraits {
6+
7+
/**
8+
* There are a few examples of traits to describe an entity of a Car below
9+
* @author Sergey Ionin
10+
*/
11+
12+
/**
13+
* Trait with abstract members and one method
14+
*/
15+
trait Car {
16+
def model: String
17+
18+
def horsePower: Int
19+
}
20+
21+
22+
/**
23+
* Another trait to be mixed into the class
24+
*/
25+
trait Marshaller {
26+
self: Car =>
27+
def toJson = s"{${"\"model\""}:$model," +
28+
s"\n${"\"horsePower\""}:$horsePower}"
29+
}
30+
31+
/**
32+
* The traits below will show that the matter of mixing matters (see the tests)
33+
*/
34+
trait Printable {
35+
self: Car =>
36+
def print(appendix: String = ""): String =
37+
s"the model $model has $horsePower HP under the hood. $appendix"
38+
}
39+
40+
trait PrettyPrintable extends Printable {
41+
self: Car =>
42+
override def print(appendix: String = "You'll definitelly enjoy driving!") = super.print(appendix)
43+
}
44+
45+
trait ShortPrintable extends Printable {
46+
self: Car =>
47+
override def print(appendix: String) = if (appendix.length() > 10) super.print() else
48+
super.print(appendix)
49+
}
50+
51+
// Implementations
52+
53+
class BMW0(val model: String, val horsePower: Int) extends Car
54+
55+
class BMWPrintable(val model: String, val horsePower: Int) extends Car with Printable {
56+
def printInfo() = print()
57+
}
58+
59+
object BMW {
60+
61+
final case class F15(model: String = "F15", horsePower: Int = 309) extends Car with Printable
62+
63+
final case class X7(model: String = "X7", horsePower: Int = 335) extends Car with Printable
64+
65+
}
66+
67+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.scala.classcompositionwithtraits
2+
3+
import org.scalatest.{Matchers, WordSpec}
4+
5+
/**
6+
* @author Sergey Ionin
7+
*/
8+
9+
class TraitsTest extends WordSpec with Matchers {
10+
11+
import com.baeldung.scala.classcompositionwithtraits.CarTraits._
12+
13+
"Class that extends Car" should {
14+
"inherit abstract class fields" in {
15+
val bmw = new BMW0("F15", 309) with Printable
16+
bmw.horsePower shouldBe 309
17+
bmw.model shouldBe "F15"
18+
bmw.print() shouldBe s"the model ${bmw.model} has ${bmw.horsePower} HP under the hood. "
19+
}
20+
}
21+
22+
"Objects that extends Car" should {
23+
"inherit abstract class fields and methods" in {
24+
val bmwX7 = BMW.X7()
25+
bmwX7.horsePower shouldBe 335
26+
bmwX7.model shouldBe "X7"
27+
bmwX7.print() shouldBe s"the model ${bmwX7.model} has ${bmwX7.horsePower} HP under the hood. "
28+
}
29+
}
30+
31+
"Classes that extends Car with Marshaller" should {
32+
"inherit abstract class fields and methods" +
33+
"and be marshallable" in {
34+
val bmw0 = new BMW0("F15", 309) with Marshaller
35+
bmw0.toJson shouldBe "{\"model\":F15,\n\"horsePower\":309}"
36+
}
37+
}
38+
39+
// in this case the print method pf the rightmost trait calls the print method of the previously mixed trait (
40+
// see super.print... in the implementation)
41+
"Classes that extends Car with PrettyPrintable with ShortPrintable" should {
42+
"behave differently depending from the mixing order" in {
43+
val bmwPrintable1 = new BMWPrintable("F15", 309) with PrettyPrintable with ShortPrintable
44+
val bmwPrintable2 = new BMWPrintable("F15", 309) with ShortPrintable with PrettyPrintable
45+
bmwPrintable1.printInfo() shouldBe s"the model ${bmwPrintable1.model} has ${bmwPrintable1.horsePower}" +
46+
s" HP under the hood. You'll definitelly enjoy driving!"
47+
bmwPrintable2.printInfo() shouldBe s"the model ${bmwPrintable1.model} has ${bmwPrintable1.horsePower} " +
48+
s"HP under the hood. "
49+
}
50+
}
51+
52+
}

0 commit comments

Comments
 (0)