SHOW:
|
|
- or go back to the newest paste.
1 | // ---------Framework:--------- | |
2 | ||
3 | abstract class Engine { | |
4 | abstract type MixIn | |
5 | // add members to this base class which are defined only in the subclass | |
6 | class Base(val children: Seq[Base]) extends MixIn | |
7 | abstract def doSomething(base: Base) | |
8 | ||
9 | final class Spec1(val foo: String, children: Seq[Base]) extends Base(children) | |
10 | final class Spec2(val bar: String, children: Seq[Base]) extends Base(children) | |
11 | } | |
12 | ||
13 | object Engine { | |
14 | def runExample(engine: Engine) { | |
15 | val spec1 = new engine.Spec1("foo", Seq.empty) | |
16 | engine.doSomething(spec1) | |
17 | val spec2 = new engine.Spec2("foo", Seq(new Spec1("foo", Seq.empty))) | |
18 | engine.doSomething(spec2) | |
19 | } | |
20 | } | |
21 | ||
22 | // ---------Client:--------- | |
23 | ||
24 | class ConcreteEngine { | |
25 | trait Height { | |
26 | val height = if(children.isEmpty()) 0 else children.map{_.height}.max | |
27 | } | |
28 | override type MixIn = Height | |
29 | ||
30 | override def doSomething(base: Base) { | |
31 | println("Node @ " + base.height) | |
32 | } | |
33 | } | |
34 | ||
35 | object Main extends App { | |
36 | Engine.run(new ConcreteEngine()) | |
37 | } |