Advantage of Using Interface in production application

93 Views Asked by At

I am programming in java for a while not much used interface. I just wondering what are the benefits of using interface. I read a article about loose coupling it states that

Tight coupling makes it much harder to add new functionality. With loose coupling, you can swap out components easily. This also makes your system more scalable as your system grows. Using loose coupling, you can safely write additional code when adding new features to your system without breaking the existing functionality.

After reading this i understood that the key take away is classes/components can be swapped easily and it helps to achieve code extensibility without breaking the existing functionality. code swapping makes sense but code extensibility without breaking changes doesn't makes sense to me.

I tried creating a interface called Player at first.

public interface Player {
  void play();
}

After creating this interface i had implemented the contracts of this interface

public class BlackPlayer implements Player{
    @Override
    public void play() {
        System.out.println("playing videos from black player");
    }
}

Finally i had created a VideoPlayer class and the constructor takes Player implemented classes.

public class VideoPlayer {
    private Player videoplayer;
    VideoPlayer(Player videoplayer){
        this.videoplayer = videoplayer;
    }
    void playVideo(){
        videoplayer.play();
    }
}

In the main method i am creating the Videoplayer instance and using its functionality

class Main{
    public static void main(String[] args) {
        VideoPlayer player = new VideoPlayer(new BlackPlayer());
        player.playVideo();
    }
}

Java Interface helps to achieve code extensibility without breaking changes, easy implementation swapping and easy unit testing(exclude about testing for now).

In future i planned to add different player i can easily swap its implementation by other class which implement the contracts of Player lets call it as Redplayer. Now i can easily swap the implementation by this line of code Before - VideoPlayer player = new VideoPlayer(new BlackPlayer()); After- VideoPlayer player = new VideoPlayer(new RedPlayer());

Correct me if i told anything wrong about implementation swapping.

Now the second thing which confused me alot.

Interface help to achieve extensible code without breaking its functionality.

Again in future all my app users are requesting a pause feature which is already in production. Now i am trying to add the additional contracts to the already created interface.

public interface Player {
    void play();
    void pause();
}

After adding this pause contract all the class which implements Player showing error that override missing for pause. By introducing the pause contract broken my existing class.

Then how can i achieve code extensibility without breaking existing functionality. How can i scale the application with new features?

1

There are 1 best solutions below

1
Jared Renzullo On

Adding a new method to an interface will break compilation for all the existing implementations of that interface. This is why for many years Java did not add any functionality to its core library classes like Collections. In Java 8 they added the ability to add a default implementation of an interface method so they could then add new methods to Collections interfaces without breaking compilation for everyone upgrading.

So to answer your question, you can always add a new method to an interface and give it a default implementation and you would not be breaking any of your existing code. The second way you can do this is to create a new interface called PausablePlayer which has the pause method. In Java you can implement multiple interfaces, so any implementations which support pause could implement that new interface in addition to the existing one. This approach works best when you want to add functionality which only applies to certain implementations but not all.