Home > Blockchain >  How should i provide views with information in android?
How should i provide views with information in android?

Time:12-16

Should i use "Tell-Don't-ask" principle to keep my class fields private? Or should i make them public and provide information directly into views?

Now i'm developing matrix calculator android application. Result of calculations is a sealed class ResultState:

sealed class ResultState {

    abstract fun showInfo(textView: TextView)

    data class SuccessState(private val matrixEntity: MatrixEntity): ResultState() {
        override fun showInfo(textView: TextView) {
            textView.text = matrixEntity.showMatrix()
        }
    }

    data class ErrorState(private val error: String = "Error"): ResultState() {
        override fun showInfo(textView: TextView) {
            textView.text = error
        }
    }

    object Default: ResultState() {
        override fun showInfo(textView: TextView) {
            textView.text = "Hello"
        }
    }
}
interface Operators {

    fun sum(matrixEntity: MatrixEntity): MatrixEntity

    fun diff(matrixEntity: MatrixEntity): MatrixEntity

    fun multiply(matrixEntity: MatrixEntity): MatrixEntity

    fun showMatrix(): String
}

data class MatrixEntity(
    private val numbers: List<List<Int>>,
) : Operators {
//code for sum, diff and multiply methods

override fun showMatrix(): String {
        var str = ""
        for (i in 0 until rows) {
            for (j in 0 until columns) {
                str  = "${returnNumber(i,j)} "
            }
            str  = "\n"
        }
        return str
    }
}

I'm trying to keep my fields private that is why i created showInfo method where i put view to write text. For me it looks ok, encapsulation works, class decides how to use his own information. Nevertheless my friend, who has commercial experience in java, says that it's incorrect way to show information. I interrupt data stream because of my decision. In addition my fields are immutable so it would be ok to make them public and give information outside the class.
I don't have a lot of experience in OOP that is why i decided to ask question on stackoverflow how should i provide views with information in android?

CodePudding user response:

I agree with your friend, you have a class that is responsible for some calculations on a matrix. But you mixed display representation with this class. Therefore, changing the view requires a change in the class. On the other hand, if the matrix information is sent to the view in an immutable form, the view will decide how to display it and the separation is done correctly.

  • Related