jeudi 4 août 2022

Removing duplicated functions in two classes in Kotlin

I have wrote the following code in Kotlin:

class ProxyiOSUIElementWrapper(val app: SIMIAppUIElementProtocol) : IAppUIElement {

    override fun tap() {
        app.tap()
    }

    override fun elementWithTestId(testId: String): IAppUIElement {
        return ProxyiOSUIElementWrapper(app.elementWithTestIdTestId(testId))
    }

    override fun table(withId: String): IAppUIElement {
        return ProxyiOSUIElementWrapper(app.tableWithId(withId))
    }

    override fun cell(withId: String): IAppUIElement {
        return ProxyiOSUIElementWrapper(app.cellWithId(withId))
    }

    override fun waitForExistence(timeout: Double) {
        app.waitForExistenceTimeout(timeout)
    }

    override fun hasText(text: String, timeout: Double) {
        app.hasTextText(text, timeout)
    }

    override fun getText(timeout: Double): String {
        return app.getTextTimeout(timeout)
    }

    override val debugDescription: String
        get() = app.debugDescription
}

class ProxyiOSAppWrapper : IAppWrapper {

    private val app = ConcreteAppWrapper(AppSetup.identifier)

    override var identifier: String = app.identifier

    override fun launch(arguments: Map<String, String>) {
        app.launchArguments(arguments as Map<Any?, *>)
    }

    override fun tap() {
        app.tap()
    }

    override fun elementWithTestId(testId: String): IAppUIElement {
        return ProxyiOSUIElementWrapper(app.elementWithTestIdTestId(testId))
    }

    override fun table(withId: String): IAppUIElement {
        return ProxyiOSUIElementWrapper(app.tableWithId(withId))
    }

    override fun cell(withId: String): IAppUIElement {
        return ProxyiOSUIElementWrapper(app.cellWithId(withId))
    }

    override fun waitForExistence(timeout: Double) {
        app.waitForExistenceTimeout(timeout)
    }

    override fun hasText(text: String, timeout: Double) {
        app.hasTextText(text, timeout)
    }

    override fun getText(timeout: Double): String {
        return app.getTextTimeout(timeout)
    }

    override val debugDescription: String
        get() = app.debugDescription
}

actual object AppSingleton {
    actual var app: IAppWrapper = ProxyiOSAppWrapper()
}

As you can see, there are functions that are being repeated, for example table, cell etc. How can I make it DRY? I want to take out these functions out of the classes and only write then once, but since both classes have to conform interfaces, I cannot do that. I tried creating one class that would inherit from both interfaces, but then there is a problem with constructors as one Proxy class takes an argument and the other does not.

Aucun commentaire:

Enregistrer un commentaire