dimanche 21 février 2021

I want a delegate object to access a private field of the main object

I want to structure my code the best way that's possible and I am facing a problem. I want to delegate the task of drawing it's data to another object. However, this other object needs to access a private field of the first object.

I have this piece of code:

class Grid {

    private val matrix = Array<Array<Int?>>(10) { Array(10) { null } }

    private val gridDraw = GridDraw(this)
    
    fun draw(canvas: Canvas) {
        gridDraw.draw(canvas)
    }
    ...
}

I want draw() method of GridDraw object to access matrix property. I can't do it if matrix is a private field and I don't want to make it public.

I don't like the idea of passing matrix inside draw method because in the future I would probably need to add more parameters to this method.

Is there a way to solve this problem?

Aucun commentaire:

Enregistrer un commentaire