Another good habit that every programmer adopt is programming to interfaces, rather than programming to implementations. It is clearly not about using interfaces in your application, though you are using interfaces at design level, it is very hard to make coders to get benefit out it. There is a very big difference between programming with interfaces and programming to interfaces. The main difference is that
To understand what I exactly mean, just look into the below example.
- programming with interfaces -design level
- programming to interfaces -coding level
To understand what I exactly mean, just look into the below example.
interface Animal {
public void eat();
}
class Human implements Animal{
public void eat(){
// Eat cakes and Buns
}
}
class Dog implements Animal{
public void eat(){
// Eat bones
}
}
If you see the above code, we just took an interface called Animal and designed another two classes by implementing the same interface. Let's now discuss where exactly the two sentences programming with interfaces and programming to interfaces comes into picture.
Programming with Interfaces :
Keeping the above code in mind, at design level (before start coding) itself one must decide weather to use interfaces or not based on the business logic. In the above example I decide to place an interface called Animal and I'll put all the common functionality in the interface. Later if you are saying that your class is said to be an Animal, you class must implement my Animal interface. Later I'l use that Interface to write my generic methods, and to create generic functionality. For ex, I can create methods like this
public makeAnimalEat( Animal animal ) {
animal.eat();
}
Dog dog = new Dog();
makeAnimalEat(dog);
//since every Dog implement interface, it is allowed to pass Dog to that method.
Another method to make a group eat,public static makeAllAnimalsEat( List<Animal> animals ) {
for (Animal animal : animals) {
animal.eat();
}
In above methods (instance or static methods) though you are not aware of what type of animal being passed to the method, you can still make the Animal eat. The above shown methods are just small examples and like wise you can create much more complex functionality with simple effort of making/planning your functionality with Interfaces. This is exactly programming with interfaces. When ever there is a possibility, go ahead and use Interface instead a Class so you won't end up in writing different methods for different Classes.
Programming to Interfaces :
The concept of programming to interface comes in to picture only at the time of creating/instancing of object. Consider you are writing code to create Room in Zoo. Each room have an Animal inside it. Let's call it as a DogRoom.
public class AnimalRoom {
// creating an animal inside Room
Dog dog = new Dog();
dog.doSomeThing();
// play with dog
}
The above code works fine and you need not worry about it. Problem arises when you planning to change the type of Animal inside the room. For example you are planning to change to a Cat. Then if you are writing Dog dog = new Dog() and when you want to change it to Cat , you have to check/change all the methods/variables of Dog to Cat. But if you program to interface
Animal animal = new Dog();
// play with animal here
When ever you need to change just change the Right Hand Side of the initialization part and you are done without any code breakage and without any compiler error.
Animal animal = new Cat();
// play with animal here
You are just done. Nothing to change other than that initialization part. That is what exactly programming to interface.
No comments:
Post a Comment