From eaf375b372c9d43a8d633b3179bbd04bddc9960c Mon Sep 17 00:00:00 2001 From: Potato Hatsue <1793913507@qq.com> Date: Fri, 25 Feb 2022 12:53:25 -0500 Subject: [PATCH] Update README --- README.md | 68 ++++++++++++++++++- .../mechdancer/dependency/TreeComponent.kt | 2 +- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3d879a1..ec785a7 100644 --- a/README.md +++ b/README.md @@ -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(), ManagedHandler by managedHandler() { +class RemoteControl : Dependent, UniqueComponent(), + ManagedHandler by managedHandler() { private val chassis: Chassis by manager.must() private val distanceSensor: DistanceSensor by manager.must() @@ -134,4 +135,67 @@ val remoteControl = robot.components.must().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 newline at end of file +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() +class BBB : Dependent, UniqueComponent() { + val manager = DependencyManager() + val aaa: AAA by manager.must() + override fun handle(dependency: Component): Boolean = manager.handle(dependency) +} +``` + +`managedHandler()`: + +```kotlin +class AAA : UniqueComponent() +class BBB : Dependent, UniqueComponent(), ManagedHandler by managedHandler() { + val aaa: AAA by manager.must() +} +``` + +## Annotation style + +The library also provides an annotation style dependency injection: + +```kotlin +class AAA : UniqueComponent() +class CCC(name: String) : NamedComponent(name) +class BBB : Dependent, UniqueComponent() { + + @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. \ No newline at end of file diff --git a/src/main/kotlin/org/mechdancer/dependency/TreeComponent.kt b/src/main/kotlin/org/mechdancer/dependency/TreeComponent.kt index 643e7ee..a63792e 100644 --- a/src/main/kotlin/org/mechdancer/dependency/TreeComponent.kt +++ b/src/main/kotlin/org/mechdancer/dependency/TreeComponent.kt @@ -18,7 +18,7 @@ abstract class TreeComponent( protected fun T.attach() = this@attach.also(this@TreeComponent._children::plusAssign) - private val ancestors: List = parent?.ancestors?.plus(parent) ?: listOf() + val ancestors: List = parent?.ancestors?.plus(parent) ?: listOf() val children = object : List by _children {}