Pergunta

I have grown into using a principle for designing and consuming interfaces that says basically, "ask for only what you need."

For instance, if I have a bunch of types that can be deleted, I'll make a Deletable interface:

interface Deletable {
   void delete();
}

Then I can write a generic class:

class Deleter<T extends Deletable> {
   void delete(T t) {
      t.delete();
   }
}

Elsewhere in code I will always ask for the smallest possible responsibility to fulfill the needs of the client code. So if I only need to delete a File, I'll still ask for a Deletable, not a File.

Is this principle that is common knowledge and already has an accepted name? Is it controversial? Is it discussed in textbooks?

Nenhuma solução correta

Licenciado em: CC-BY-SA com atribuição
scroll top