YAPC::EU 2018 Glasgow Update!
This commit is contained in:
parent
22f33d4004
commit
4e2d22a71d
1170 changed files with 15042 additions and 3047 deletions
32
Task/Delegates/Scala/delegates.scala
Normal file
32
Task/Delegates/Scala/delegates.scala
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
trait Thingable {
|
||||
def thing: String
|
||||
}
|
||||
|
||||
class Delegator {
|
||||
var delegate: Thingable = _
|
||||
|
||||
def operation: String = if (delegate == null) "default implementation"
|
||||
else delegate.thing
|
||||
}
|
||||
|
||||
class Delegate extends Thingable {
|
||||
override def thing = "delegate implementation"
|
||||
}
|
||||
|
||||
// Example usage
|
||||
// Memory management ignored for simplification
|
||||
object DelegateExample extends App {
|
||||
|
||||
val a = new Delegator
|
||||
assert(a.operation == "default implementation")
|
||||
// With a delegate:
|
||||
val d = new Delegate
|
||||
a.delegate = d
|
||||
assert(a.operation == "delegate implementation")
|
||||
// Same as the above, but with an anonymous class:
|
||||
a.delegate = new Thingable() {
|
||||
override def thing = "anonymous delegate implementation"
|
||||
}
|
||||
assert(a.operation == "anonymous delegate implementation")
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue