Skip to content

Commit a7b5c7a

Browse files
Merge pull request #1456 from mdipirro/scala-601-stackable-trait
SCALA-601 Add code for stackable trait article
2 parents 2ff7d51 + 8398deb commit a7b5c7a

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-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+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.scala.stackabletrait
2+
3+
object StackableTraitWithExplicitBaseAndCoreExample {
4+
trait BaseIntTransformation {
5+
def transform(value: Int): Int
6+
}
7+
8+
trait CoreIntTransformation extends BaseIntTransformation {
9+
def transform(value: Int): Int = value
10+
}
11+
12+
trait DoubleTransformation extends CoreIntTransformation {
13+
override def transform(value: Int): Int =
14+
super.transform(value * 2)
15+
}
16+
17+
trait LogInt extends CoreIntTransformation {
18+
override def transform(value: Int): Int = {
19+
println(s"Transforming value: $value")
20+
super.transform(value)
21+
}
22+
}
23+
24+
trait CustomTransformation(f: Int => Int) extends CoreIntTransformation {
25+
override def transform(value: Int): Int =
26+
super.transform(f(value))
27+
}
28+
29+
@main
30+
def mainSTE(): Unit = {
31+
val logAndDouble = new CoreIntTransformation
32+
with DoubleTransformation
33+
with LogInt {}
34+
val doubleAndLog = new CoreIntTransformation
35+
with LogInt
36+
with DoubleTransformation {}
37+
val logAndCustom = new CoreIntTransformation
38+
with CustomTransformation(_ + 1)
39+
with LogInt {}
40+
41+
println(s"Log and double: ${logAndDouble.transform(5)}")
42+
println(s"Double and log: ${doubleAndLog.transform(5)}")
43+
println(s"Log and increment: ${logAndCustom.transform(5)}")
44+
}
45+
}

0 commit comments

Comments
 (0)