Object Oriented Design Principle: Programming for Interface NOT Implementation

Programming for interface not implementation

Programming for interface not implementation principle will lead to code flexibility. Most important purpose of interface is polymorphism.  By polymorphism, java can handle objects dynamically at runtime.

Okay, let’s take a very simple example for you to realize how this principle will make your code flexible!

Suppose we have already a Tiger and Lion class.

And we have this Zoo class


import java.util.ArrayList;

public class Zoo {
    private ArrayList animalList;
    
    public Zoo() {
        animalList = new ArrayList();
    }
    
    public void addAnimal( Lion lion ) {
        animalList.add( lion );
    }
    
    public void addAnimal( Tiger tiger ) {
        animalList.add( tiger );
    }
}

The animal list will hold all animals in the zoo. And we have to overload addAnimal() method for adding each animal. But wait, what if we add zebra to be added in the zoo?

Solution #1 – it’s pretty easy add another addAnimal method takes Zebra object.


import java.util.ArrayList;

public class Zoo {
    private ArrayList animalList;
    
    public Zoo() {
        animalList = new ArrayList();
    }
    
    public void addAnimal( Lion lion ) {
        animalList.add( lion );
    }
    
    public void addAnimal( Tiger tiger ) {
        animalList.add( tiger );
    }

    public void addAnimal( Zebra zebra ) {
        animalList.add( zebra );
    }
}

Cool! But the zoo has expanded! The zoo can accommodate hundreds of animals.

Here’s your code now!


import java.util.ArrayList;

public class Zoo {
    private ArrayList animalList;
    
    public Zoo() {
        animalList = new ArrayList();
    }
    
    public void addAnimal( Lion lion ) {
        animalList.add( lion );
    }
    
    public void addAnimal( Tiger tiger ) {
        animalList.add( tiger );
    }

    public void addAnimal( Zebra zebra ) {
        animalList.add( zebra );
    }

    public void addAnimal( Monkey monkey ) {
        animalList.add( monkey );
    }

    public void addAnimal( Crocodile crocodile ) {
        animalList.add( crocodile );
    }

    public void addAnimal( Eagle eagle ) {
        animalList.add( eagle );
    }
    //100 MORE ANIMALS!
}

What the heck! 1000 lines of code full of duplications!

this-is-why-i-bomb-people

The code above clearly violates not only programming for interface not implementation principle but also  DRY Principle and Open-Closed Principle.

Every time we add an animal we need also to change the code for the Zoo class. But how can we solve this?

Interface to the rescue!

Create an interface Animal and let all animal concrete classes to implement it.


public class Tiger implements Animal {

} 

//for the rest of the animals also

And here’s our new Zoo class.



import java.util.ArrayList;

public class Zoo {
    private ArrayList animalList;
    
    public Zoo() {
        animalList = new ArrayList();
    }
    
    public void addAnimal( Animal animal ) {
        animalList.add( animal );
    }
}


In that case, Zoo class doesn’t have to know what type of animal to be added in the list. In the other words, you can create another animal-specific class without changing our code! Isn’t awesome?

You just made your code flexible!

I apologize for the simplicity of the example 🙂

For more design principle, just click here.

If you liked or disliked with this blog post, feel free leave a comment below. I really love to hear critics.

Don’t forget to share it and follow my blog for more updates.

Advertisements

3 thoughts on “Object Oriented Design Principle: Programming for Interface NOT Implementation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s