Interface Segregation Principle: Your only way to be a super competent developer

What is Interface Segregation Interface?

Bad Practices

interface IMultiFunction {
public void print();
public void getPrintSpoolDetails();
public void scan();
public void scanPhoto();
public void fax();
public void internetFax();
}
class CanonPrinter implements IMultiFunction {

@Override
public void print() {}

@Override
public void getPrintSpoolDetails() {}

/* This machine can't scan */
@Override
public void scan() {}

/* This machine can't scan photo */
@Override
public void scanPhoto() {}

/* This machine can't fax */
@Override
public void fax() {}

/* This machine can't fax on internet */
@Override
public void internetFax() {}

}

Good Practices

interface IPrint {
public void print();
public void getPrintSpoolDetails();
}

interface IScan {
public void scan();
public void scanPhoto();
}

interface IFax {
public void fax();
public void internetFax();
}
class HPPrintNScanner implements IPrint, IScan {

@Override
public void scan() {
// TODO Auto-generated method stub

}

@Override
public void scanPhoto() {
// TODO Auto-generated method stub

}

@Override
public void print() {
// TODO Auto-generated method stub

}

@Override
public void getPrintSpoolDetails() {
// TODO Auto-generated method stub

}

}

Techniques to identify violations

  1. Find if your Interfaces have methods with low cohesion
  2. Check Empty Method Implementations

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
KD Knowledge Diet

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!