Skip to content

Commit a560b87

Browse files
Merge pull request Kotlin#50 from p7nov/delegation-revised
docs: revised Delegation
2 parents a8a205c + 33ff997 commit a560b87

File tree

2 files changed

+31
-37
lines changed

2 files changed

+31
-37
lines changed
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
# Delegation Pattern
22

3+
Kotlin supports easy implementation of the [delegation pattern](https://kotlinlang.org/docs/reference/delegation.html) on the native level without any boilerplate code.
4+
35
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
46

57
```kotlin
6-
interface SoundBehaviour { // 1
8+
interface SoundBehavior { // 1
79
fun makeSound()
810
}
911

10-
class ScreamBehavior(val n:String): SoundBehaviour { // 2
12+
class ScreamBehavior(val n:String): SoundBehavior { // 2
1113
override fun makeSound() = println("${n.toUpperCase()} !!!")
1214
}
1315

14-
class RockAndRollBehavior(val n:String): SoundBehaviour { // 2
16+
class RockAndRollBehavior(val n:String): SoundBehavior { // 2
1517
override fun makeSound() = println("I'm The King of Rock 'N' Roll: $n")
1618
}
1719

1820
// Tom Araya is the "singer" of Slayer
19-
class TomAraya(n:String): SoundBehaviour by ScreamBehavior(n) // 3
21+
class TomAraya(n:String): SoundBehavior by ScreamBehavior(n) // 3
2022

2123
// You should know ;)
22-
class ElvisPresley(n:String): SoundBehaviour by RockAndRollBehavior(n) // 3
24+
class ElvisPresley(n:String): SoundBehavior by RockAndRollBehavior(n) // 3
2325

2426
fun main() {
2527
val tomAraya = TomAraya("Trash Metal")
@@ -31,12 +33,10 @@ fun main() {
3133

3234
</div>
3335

34-
In Kotlin it's easy to delegate method calls without any boilerplate.
3536

36-
1. The interface SoundBehaviour is defined. Later there will be one class that implements the method and another will
37-
also have the interface, but just delegates the method call.
38-
2. The class ScreamBehavior and RockAndRollBehavior actually implement the method.
39-
3. The class TomAraya and ElvisPresley just delegate the methods defined by the interface SoundBehaviour to the
40-
responsible implementation. But does not need any boilerplate to delegate the method call at all.
41-
4. Call makeSound() on tomAraya of type TomAraya or elvisPresley of type ElvisPresley and it is delegated to the
42-
associated class
37+
1. Defines the interface `SoundBehavior` with one method.
38+
2. The classes `ScreamBehavior` and `RockAndRollBehavior` implement the interface and contain their own implementations of the method.
39+
3. The classes `TomAraya` and `ElvisPresley` also implement the interface, but not the method. Instead, they delegate the method calls to the
40+
responsible implementation. The delegate object is defined after the `by` keyword. As you see, no boilerplate code is required.
41+
4. When `makeSound()` is called on `tomAraya` of type `TomAraya` or `elvisPresley` of type `ElvisPresley`, the call is delegated to the
42+
corresponding delegate object.

examples/07_Delegation/02_DelegatedProperties.md

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
# Delegated Properties
22

3-
There's some new syntax: you can say `val 'property name': 'Type' by 'expression'`.
4-
The expression after `by` is the delegate, because `get()` and `set()` methods
5-
corresponding to the property will be delegated to it.
6-
Property delegates don't have to implement any interface, but they have
7-
to provide methods named `getValue()` and `setValue()` to be called. The full documentation is [here](http://kotlinlang.org/docs/reference/delegated-properties.html).
8-
9-
Let's try some code.
3+
Kotlin provides a mechanism of [delegated properties](http://kotlinlang.org/docs/reference/delegated-properties.html) that allows delegating the calls of the property `set` and `get` methods to a certain object.
4+
The delegate object in this case should have the method `getValue`. For mutable properties, you'll also need `setValue`.
105

116
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
127

@@ -38,47 +33,46 @@ fun main() {
3833

3934
</div>
4035

41-
1. Delegated property `p` of type `String`
42-
2. Delegation methods. For immutable property only `getValue` is required.
43-
44-
### Standard delegates
36+
1. Delegates property `p` of type `String` to the instance of class `Delegate`. The delegate object is defined after the `by` keyword.
37+
2. Delegation methods. The signatures of these methods are always as shown in the example. Implementations may contain any steps you need. For immutable properties only `getValue` is required.
4538

46-
Kotlin standard library contains bunch of useful delegates, like `lazy`, `observable`, etc.
39+
### Standard Delegates
4740

48-
For example `lazy` is used in case lazy initialization.
41+
The Kotlin standard library contains a bunch of useful delegates, like `lazy`, `observable`, and other. You may use them as is.
42+
For example `lazy`is used for lazy initialization.
4943

5044
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
5145

5246
```kotlin
5347
class LazySample {
5448
init {
55-
println("created!"); // 1
49+
println("created!") // 1
5650
}
5751

58-
val lazy: String by lazy {
52+
val lazyStr: String by lazy {
5953
println("computed!") // 2
6054
"my lazy"
6155
}
6256
}
6357

6458
fun main() {
6559
val sample = LazySample() // 1
66-
println("lazy = ${sample.lazy}") // 2
67-
println("lazy = ${sample.lazy}") // 3
60+
println("lazyStr = ${sample.lazyStr}") // 2
61+
println(" = ${sample.lazyStr}") // 3
6862
}
6963
```
7064

7165
</div>
7266

7367
1. Property `lazy` is not initialized on object creation.
74-
2. The first call to `get()` executes the lambda expression passed to lazy() as an argument and remembers the result
75-
3. Subsequent calls to `get()` simply return the remembered result.
68+
2. The first call to `get()` executes the lambda expression passed to `lazy()` as an argument and saves the result.
69+
3. Further calls to `get()` return the saved result.
7670

77-
If you want thread safety, use blockingLazy() instead: it guarantees that the values will be computed only in one thread, and that all threads will see the same value.
71+
If you want thread safety, use `blockingLazy()` instead: it guarantees that the values will be computed only in one thread and that all threads will see the same value.
7872

79-
### Properties in map
73+
### Storing Properties in a Map
8074

81-
Properties stored in a map. This comes up a lot in applications like parsing JSON
75+
Property delegation can be used for storing properties in a map. This is handy for tasks like parsing JSON
8276
or doing other "dynamic" stuff.
8377

8478
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
@@ -101,6 +95,6 @@ fun main() {
10195

10296
</div>
10397

104-
1. Delegates take values from the `map` (by the string keys - names of properties).
98+
1. Delegates take values from the `map` by the string keys - names of properties.
10599

106-
Of course, you can have mutable property as well, that will modify the map upon assignment (note that you'd need `MutableMap` instead of read-only `Map`).
100+
You can delegate mutable properties to a map as well. In this case, the map will be modified upon property assignments. Note that you will need `MutableMap` instead of read-only `Map`.

0 commit comments

Comments
 (0)