How to implement SOLID on an interface segregation

StuartDTO :

I have an interface like this :

interface ClientRequestListener {
    fun onLoadStub(name: String)
    fun onClientNeeded(id: String, email: String)
}

But then when implementing it on one class I just need the onClientNeeded is there any way to avoid having to override these two methods? I've tried to create another interface that extends that one but still asking me to implement the methods..

Clarification

I'm using an external library, that uses an interface with these two methods, and the thing is where I'm using this I just need one method instead of both, and instead of having an override doing anything I just wanted to know a way to avoid that extra override that I do not need.

EDIT

fun doSomeMagic(name: String, clientRequestListener: ClientRequestListener? = null) =
    LibraryRequest.getClient(name)
        .withListener(object : LibraryInterface() {
            override fun onLoadStub(name: String) {
                clientRequestListener?.onLoadStub(name)
            }

            override fun onClientNeeded(id: String, email: String) {
                clientRequestListener?.onClientNeeded(id, email)
            }
        })

So when I'm using this method I do something like this :

doSomeMagic("Charls", object : ClientRequestListener {
  override fun onClientNeeded(name: String) {
    //I'm not doing nothing here
  }

  override fun onClientNeeded(id: String, email: String) {
    //Do something with the id and email
  }
})

So what I'd like to do is something to call only onClientNeeded there instead both of them.

Demigod :

You can change things in a several options here:

a) You can create an abstract class which will provide default (empty) implementation of the unneeded method, and will force you to override only method you're interested in:

abstract class AbsClientRequestListener {
    override fun onLoadStub(name: String) {
    }
    abstract fun onClientNeeded(id: String, email: String)
}

b) If you have a hierarachy of classes, you can provide a default implementation in your base class and leave onClientNeeded unimplemented for the subclasses:

interface ClientRequestListener {
    fun onLoadStub(name: String)
    fun onClientNeeded(id: String, email: String)
}

abstract class BaseClient: ClientRequestListener {
    override fun onLoadStub(name: String) {
        // Leave empty
    }
}

class Client:BaseClient() {
    override fun onClientNeeded(id: String, email: String) {
        // Put required implementation
    }
}

c) Or you can just use this interface as is and provide an empty implementation every time :)

EDIT

In your specific case you can add class ClientRequestListenerAdapter:

abstract class ClientRequestListenerAdapter : ClientRequestListener {
    override fun onLoadStub(name: String) {
    }
}

And use it like this:

doSomeMagic("Charls", object : ClientRequestListenerAdapter {
  override fun onClientNeeded(id: String, email: String) {
    //Do something with the id and email
  }
})

No changes in the function doMagic are needed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=349223&siteId=1