Skip to content

Commit c499c26

Browse files
author
Matteo Di Pirro
committed
SCALA-601 Add code for stackable trait article
1 parent 07c3256 commit c499c26

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.scala.stackabletrait
2+
3+
object DecoratorExample {
4+
trait IntTransformation {
5+
def transform(value: Int): Int = value
6+
}
7+
8+
class TransformationDecorator(wrappee: IntTransformation)
9+
extends IntTransformation {
10+
override def transform(value: Int): Int = wrappee.transform(value)
11+
}
12+
13+
class DoubleDecorator(wrappee: IntTransformation)
14+
extends TransformationDecorator(wrappee) {
15+
override def transform(value: Int): Int =
16+
super.transform(value * 2)
17+
}
18+
19+
class LogInt(wrappee: IntTransformation)
20+
extends TransformationDecorator(wrappee) {
21+
override def transform(value: Int): Int = {
22+
println(s"Transforming value: $value")
23+
super.transform(value)
24+
}
25+
}
26+
27+
class CustomDecorator(f: Int => Int, wrappee: IntTransformation)
28+
extends TransformationDecorator(wrappee) {
29+
override def transform(value: Int): Int =
30+
super.transform(f(value))
31+
}
32+
33+
@main
34+
def mainDec(): Unit = {
35+
val identity = new IntTransformation {}
36+
37+
val withLogging = new LogInt(identity)
38+
val withDouble = new DoubleDecorator(withLogging)
39+
val withCustom = new CustomDecorator(_ + 1, withDouble)
40+
41+
println(s"With increment, double, and logging: ${withCustom.transform(5)}")
42+
}
43+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.scala.stackabletrait
2+
3+
object MixinExample {
4+
trait Person {
5+
val name: String
6+
val country: String
7+
}
8+
9+
case class ItalianPerson(name: String) extends Person {
10+
val country = "Italy"
11+
}
12+
13+
trait WithPrettyPrinting extends Person {
14+
def prettyPrint: String =
15+
s"""Name: $name
16+
|Country: $country""".stripMargin
17+
}
18+
19+
@main
20+
def main(): Unit =
21+
val italian = new ItalianPerson("Mario") with WithPrettyPrinting
22+
println(italian)
23+
println(italian.prettyPrint)
24+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.scala.stackabletrait
2+
3+
object StackableTraitExample {
4+
trait IntTransformation {
5+
def transform(value: Int): Int = value
6+
}
7+
8+
trait DoubleTransformation extends IntTransformation {
9+
override def transform(value: Int): Int =
10+
super.transform(value * 2)
11+
}
12+
13+
trait LogInt extends IntTransformation {
14+
override def transform(value: Int): Int = {
15+
println(s"Transforming value: $value")
16+
super.transform(value)
17+
}
18+
}
19+
20+
trait CustomTransformation(f: Int => Int) extends IntTransformation {
21+
override def transform(value: Int): Int =
22+
super.transform(f(value))
23+
}
24+
25+
@main
26+
def mainST(): Unit = {
27+
val logAndDouble = new IntTransformation
28+
with DoubleTransformation
29+
with LogInt {}
30+
val doubleAndLog = new IntTransformation
31+
with LogInt
32+
with DoubleTransformation {}
33+
val logAndCustom = new IntTransformation
34+
with CustomTransformation(_ + 1)
35+
with LogInt {}
36+
37+
println(s"Log and double: ${logAndDouble.transform(5)}")
38+
println(s"Double and log: ${doubleAndLog.transform(5)}")
39+
println(s"Log and increment: ${logAndCustom.transform(5)}")
40+
}
41+
}

0 commit comments

Comments
 (0)