mardi 23 mars 2021

How to solve this strong dependency between those classes?

I have two classes A and B.

Class A has several properties. One of them are an instance of class B.

At some point in the main function I will define an instance a of A. I will need to do some computation on its property of type B. This computation, however, needs another property of a.

The result is a.property3.computation(a.property1,someValue). I think it's ugly.

Here is some "pseudo-code" (in Kotlin but I am facing the same problem using other languages as well):

class B {
    val property : Map<String,Int>
    
    fun computation(val parameter1: Int, val parametre2: Double) : Int {
        //doing some computation
        return result
    }
}

class A {
    var property1 : Int 
    var property2 : Stirng 
    var property3 : B 
}

fun main (){
    val someValue = 0.4 //It's here to show the idea that the function `computation` needs also some other arguments that does not come from `A`'s propery
    val a = A()
    val result = a.property3.computation(a.property1,someValue)  // here is the line that bothers me !
}

Aucun commentaire:

Enregistrer un commentaire