Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
berberman committed Feb 25, 2022
1 parent ca87824 commit eaf375b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
68 changes: 66 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ It is unique component, and can measure the distance to wall. A remote control c
robot:

```kotlin
class RemoteControl : Dependent, UniqueComponent<RemoteControl>(), ManagedHandler by managedHandler() {
class RemoteControl : Dependent, UniqueComponent<RemoteControl>(),
ManagedHandler by managedHandler() {
private val chassis: Chassis by manager.must()
private val distanceSensor: DistanceSensor by manager.must()

Expand Down Expand Up @@ -134,4 +135,67 @@ val remoteControl = robot.components.must<RemoteControl>().translateRobot(x)
```

We set up everything to the `DynamicScope`, and the scope will help us deal with all dependencies.
No references passed through constructors, nor worries about instantiation orders!
No references passed through constructors, nor worries about instantiation orders!

## Manager style

We have seen how the manager was used to declare dependency. `ManagedHandler by managedHandler()` is
trivial, and the following two code snippets are identical:

Manually:

```kotlin
class AAA : UniqueComponent<AAA>()
class BBB : Dependent, UniqueComponent<BBB>() {
val manager = DependencyManager()
val aaa: AAA by manager.must()
override fun handle(dependency: Component): Boolean = manager.handle(dependency)
}
```

`managedHandler()`:

```kotlin
class AAA : UniqueComponent<AAA>()
class BBB : Dependent, UniqueComponent<BBB>(), ManagedHandler by managedHandler() {
val aaa: AAA by manager.must()
}
```

## Annotation style

The library also provides an annotation style dependency injection:

```kotlin
class AAA : UniqueComponent<AAA>()
class CCC(name: String) : NamedComponent<CCC>(name)
class BBB : Dependent, UniqueComponent<BBB>() {

@Must
lateinit var aaa: AAA

@Maybe
@Name("ccc1")
var ccc: CCC? = null

@Must
lateinit var ccc2: CCC

private val injector by annotatedInjector()

override fun handle(dependency: Component): Boolean = injector.handle(dependency)

}

scope {
setup(AAA())
setup(CCC("ccc1"))
setup(CCC("ccc2"))
setup(BBB())
}
```

We need use `annotatedInjector()` to create an injector for the dependent, and manually
delegate `hanlde` method to the injector. `@Must` declares a strict dependency with the type of the
field, and `@Maybe` declares a weak dependency. `@Name` can specify dependency's name. Field's name
will be used if no `@Name` annotated.
2 changes: 1 addition & 1 deletion src/main/kotlin/org/mechdancer/dependency/TreeComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class TreeComponent(
protected fun <T : TreeComponent> T.attach() =
this@attach.also(this@TreeComponent._children::plusAssign)

private val ancestors: List<TreeComponent> = parent?.ancestors?.plus(parent) ?: listOf()
val ancestors: List<TreeComponent> = parent?.ancestors?.plus(parent) ?: listOf()
val children = object : List<TreeComponent> by _children {}


Expand Down

0 comments on commit eaf375b

Please sign in to comment.