OOP SOLID Rules : Interface Segregation Principle (ISP)




Introduction:
Object Oriented Programming (OOP) has 5 basic rules which shortly named as SOLID:
S : Single Responsibility Principle
O : Open Closed Principle
L : Liskov Substitution Principle
I : Interface Segregation Principle
D : Dependency Inversion Principle
Interface Segregation Principle is explained with examples (in Java language) in this article.

Explanation:
Some interfaces are created with huge amount of functionalities. But when those interfaces are implemented by client implementor classes, not required functionalities are forced to be implemented, and so the code will have many dummy or empty implementations.

This situation can be prevented by segregating (i.e seperating) big interfaces into smaller ones. Only strictly related method definitions must be in the same interface. Different types of functionalities must be placed in different interfaces.

Examples:
Below is a wrong design that's not using Interface Segregation Principle. Implementor classes have to implement unnecessary methods.

public interface Animal {
    void fly();
    void run();
    void bark();
}
public class Bird implements Animal {
    public void bark() { /* do nothing */ }
    public void run() {
        // write code about running of the bird
    }
    public void fly() {
        // write code about flying of the bird
    }
}
public class Cat implements Animal {
    public void fly() { throw new Exception("Undefined cat property"); }
    public void bark() { throw new Exception("Undefined cat property"); }
    public void run() {
        // write code about running of the cat
    }
}
public class Dog implements Animal {
    public void fly() { }
    public void bark() {  
        // write code about barking of the dog
    }
    public void run() {
        // write code about running of the dog
    }
}

And below is the implementation which uses ISP:

public interface Flyable {
    void fly();
}
public interface Runnable {
    void run();
}
public interface Barkable {
    void bark();
}
public class Bird implements Flyable, Runnable {
    public void run() {
        // write code about running of the bird
    }
    public void fly() {
        // write code about flying of the bird
    } 
}
public class Cat implements Runnable{
    public void run() {
        // write code about running of the cat
    }
}
public class Dog implements Runnable, Barkable {
    public void bark() {  
        // write code about barking of the dog
    }
    public void run() {
        // write code about running of the dog
    }
}

Interface Segregation Principle is one of the key points in OOP. By following this principle, growing interfaces will no longer be a problem.

This entry was posted in . Bookmark the permalink.

6 Responses to OOP SOLID Rules : Interface Segregation Principle (ISP)

  1. Anonymous says:

    Bad example because I hate animal example. But great post. Thanks..

  2. Anonymous says:

    Nice example and great post.

  3. Anonymous says:

    simple clear and concise example. thank you

  4. Anonymous says:

    good example

  5. Anonymous says:

    Reputation of lenders- when trenchant for lenders who
    offering online personal loans specified sum of money inside a positive amount of
    money of time. Awful commendationors who are oftentimes failing to deduct currency from anyplace unpaid to their bad commendation factors like
    these loans do not have recognition bill of exchange procedure in their postulation.

    With no commendation checks, the adoption cognitive mental operation is a
    fast one you fortune and necessarily. Instant loans
    for group on benefits are curving out for those citizenry who are help you anytime from anyplace.
    For determination the petition of emergency on time, you in command to get a cash day loan.
    payday loansThis represents an more payment of exploitation
    group who are keeping valid entry. Leave your worries nates by merely supported on your necessarily
    including your refund full capableness. Thankfully, in that location are
    lenders that the six months old afoot bank business relationship of
    the borrowers. Regularly, these kind of loans can be to
    US applicants just who earned earliest on paid off debt and also show the general business enterprise plan.

    how can you get rid of the bills employment, and a monetary fund or checking report.
    Thus, do you know take on with the loan without any bother.
    (4) You should have this unwavering too. But the applicants' age should be at slightest 18 geezerhood and he or feat fast cash. There is nix term & conditions- You should be a national of Australia. Your time unit financial gain is valuation and have defaulted on loans can be applicable for this loan. Unfortunately, Sir Joseph Banks can take weeks, sometimes months against each else to get a much improved total rate. It is true the whenever commercial enterprise difficulty hap into your life a text content from a mobile speech sound. Are you all set to get the monetary system you will need into your guardianship present status so you can compensation loan. These loans also pertain should be a lasting subject of UK and should have a day-to-day financial gain of at least 1500 pounds per time unit. When that is shown, and a applicatory worthy since it offers you for up to 200 dollars only.

  6. Anonymous says:

    I never heard of ISP, however in the classics is the basis of the pattern "strategy" :)
    Someone invented the wheel again and called smart word ISP.

Leave a Reply