Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何解决Kotlin中的循环导入_Kotlin_Design Patterns_Circular Dependency - Fatal编程技术网

如何解决Kotlin中的循环导入

如何解决Kotlin中的循环导入,kotlin,design-patterns,circular-dependency,Kotlin,Design Patterns,Circular Dependency,我对Kotlin的编程还不熟悉,我已经遇到了经典的循环依赖问题——我知道Kotlin可以处理这些问题,但我想知道如何改变我的设计以避免它。我应该在下面使用什么结构或Kotlin功能 import MyClass interface MyInterface { fun useMyClass(myInstance: MyClass) } 并在MyClass中的某个地方调用它: ImplementingMyInterfaceClass().useMyClass(this) 技术上,我可以

我对Kotlin的编程还不熟悉,我已经遇到了经典的循环依赖问题——我知道Kotlin可以处理这些问题,但我想知道如何改变我的设计以避免它。我应该在下面使用什么结构或Kotlin功能

import MyClass

interface MyInterface {
    fun useMyClass(myInstance: MyClass)
}
并在
MyClass
中的某个地方调用它:

ImplementingMyInterfaceClass().useMyClass(this)
<>技术上,我可以在中间创建另一个构造,这将被<代码> MySudio使用,并通过<代码> MyClass < /代码>继承/实现,但这只是感觉不正确。有什么建议吗


注释:在我的特定问题中,考虑<代码> MyPosie作为一种“修饰符”(因为它将修改类的实例)可能是有帮助的。-

MyClass
实例应该知道它的修饰符,每个修饰符都应该能够修改该实例。

这在很大程度上取决于接口必须做什么,但您可以将其函数参数限制在MyClass实现的某个接口上:

interface MyInterface {
    fun increaseSomeValue(someValueHolder: MySubInterface)

    interface MySubInterface {
        var myValue: Int
    }
}

class MyClass(myList: List<MyInterface>): MyInterface.MySubInterface {
    val storedList: List<myInterface> = myList
    override var myValue: Int = 10
}

嘿,谢谢你的回答:)我喜欢这些解决方案,但不幸的是,在我的例子中,接口必须自己访问类实例,而不是其中的字段。如果将接口的每个实现视为一种“修饰符”(因为它将修改类的实例)-<代码> MyClass 实例应该知道它的修饰符,并且每个修饰符应该能够修改该实例。我仍在理解你的答案,需要仔细阅读你在这里分享的一些语法,但乍一看,它并没有涵盖我的具体问题。但是,如果没有其他选择,我将接受你的答案,因为它为这个问题提供了很好的总体指导-希望你不介意稍等片刻!
ImplementingMyInterfaceClass().useMyClass(this)
interface MyInterface {
    fun increaseSomeValue(someValueHolder: MySubInterface)

    interface MySubInterface {
        var myValue: Int
    }
}

class MyClass(myList: List<MyInterface>): MyInterface.MySubInterface {
    val storedList: List<myInterface> = myList
    override var myValue: Int = 10
}
interface MyInterface {
    fun increaseSomeValue(someValue: KMutableProperty<Int>)
}

class MyInterfaceImpl: MyInterface {
    override fun increaseSomeValue(someValue: KMutableProperty<Int>) {
        someValue.setter.call(someValue.getter.call() + 10)
    }
}

// from MyClass:
storedList.first().printSomeValue(::myValue)
interface MyInterface {
    fun printSomeValue(valueProvider: () -> Int)
}

class MyInterfaceImpl: MyInterface {
    override fun printSomeValue(valueProvider: () -> Int) {
        println(valueProvider())
    }
}

// from MyClass:
storedList.first().printSomeValue(::myValue)
// or
storedList.first().printSomeValue { 1..10.random() }