s-hunter
s-hunter

Reputation: 25826

Any examples of bad uses of interface?

I want to avoid use interface when it's not needed. For example, there is an interface AAA, and there is a class AAAImpl implementing it, this interface AAA is only being implemented by AAAImpl, and AAAImpl is only implementing one interface, which is this AAA. The argument of doing this is the code is decoupled, it will be easier for unit testing, it leaves more options in the future for adding more features, etc.

Are these arguments valid?

Upvotes: 1

Views: 1738

Answers (2)

Marko Krstic
Marko Krstic

Reputation: 1447

One of the bad usege of interface is when iterface has more then it's needed and classes which implement the interface will have to implement all those methods which normally shouldn't.

For example, we have interface for printing and we have many different version of printing (from html document without footer, from html doc with footer, from pdf etc).

So our interface will look like:

public interface IPrinter{
    public void printHtmlWithFooter();
    public void printHtmlWithoutFooter();
    public void printPdf();
}

and then you have implementations:

public class HtmlPrinter implements IPrinter{

    public void printHtmlWithFooter(){
        // some code, printing ....
    }

    public void printHtmlWithoutFooter(){
        // some code, printing ....
    }

    public void printPdf(){}
}


public class PdfPrinter implements IPrinter{

    public void printHtmlWithFooter(){}
    public void printHtmlWithoutFooter(){}

    public void printPdf(){
        // some code, printing ....
    }
}

as you see, you need to implement all of these methods in each class even if they are completely empty. Let's imagine that you have 10 different classes which implement IPrinter interface and you want to add one additional method into interface, so you need to do implementation in each of these classes. That would be one of examples how you should not use interfaces.

Instead of that you should have only:

public interface IPrinter{
    public void print();
}

and then client doesn't care how it will be printed and what, he only knows that method print() has to be called and that's it. Concrete classes should take care about concrete implementation.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

One class implementing one interface is a perfectly valid strategy for designing a class library, as long as the users of your class have no direct access to the implementing class. This is information hiding at its best: the users see what they need to see, while you keep an ability to redesign your implementation in more ways than you could if you let the users access the implementing class directly.

This also gives your users the flexibility to test their code without relying on any of your code outside the interface definition.

Overall, it is a win-win situation with no downsides.

As far as bad uses of interfaces go, there is a number of possibilities:

  • Interfaces that attempt to do too much - Adding an interface that covers every single method of a class that performs many different tasks is a bad idea, with the exception of "infrastructure interfaces", e.g. interfaces required to define remoting.
  • Interfaces that attempt to do too little - Such interfaces cover a small part of functionality of the class, without enabling a meaningful task to be performed without making a reference to the implementing class.
  • Interfaces that provide a poor match for the functionality of the class - for example, adding IComparable<T> or IEquitable<T> to a mutable class.

Upvotes: 3

Related Questions