Java Interface Requirement

I have two programs. The first one does not have a Buffer interface, and the second one does not have a Buffer interface.
Both programs give me the same result.
My question is.
Why do I need to create a "Buffer" interface, while I can get the same result without an interface?
Here are my two programs:

Without interface:

public class MainClass { public static class producer { maintainBuffer bf; producer(maintainBuffer buffer) { this.bf = buffer; } public void setData() { for (int i = 0; i <= 10; i++) { bf.set(i); } } } public static class reader { maintainBuffer bf; reader(maintainBuffer buffer) { this.bf = buffer; } public void getData() { System.out.println("From Reader Class:-" + bf.get()); } public void extraData() { System.out.println("Extra DATA"); } } public static class maintainBuffer { int buffer = -1; public int get() { return buffer; } public void set(int a) { buffer += a; } } public static void main(String str[]) { maintainBuffer b = new maintainBuffer(); producer p = new producer(b); p.setData(); reader r = new reader(b); r.getData(); r.extraData(); } } 

With interface

 public class MainClass { public static class producer { Buffer bf; producer(Buffer buffer) { this.bf = buffer; } public void setData() { for (int i = 0; i <= 10; i++) { bf.set(i); } } } public static class reader { Buffer bf; reader(Buffer buffer) { this.bf = buffer; } public void getData() { System.out.println("From Reader Class:-" + bf.get()); } public void extraData() { System.out.println("Extra DATA"); } } public static interface Buffer{ public int get(); public void set(int a); } static class maintainBuffer implements Buffer{ int buffer=-1; public int get() { return buffer; } public void set(int a) { buffer +=a; } } public static void main(String str[]) { Buffer b = new maintainBuffer(); producer p = new producer(b); p.setData(); reader r = new reader(b); r.getData(); r.extraData(); } } 
+4
source share
3 answers

With the interface, you can use other implementations later without changing all your code. For instance. if you use the List interface, it could be LinkedList, ArrayList, CopyOnWriteArrayList or something else.

You explain, implementation details are not important.

On the other hand, I would not create an interface for each class. In most cases, adding an interface later in the unlikely event that you really need it is not always difficult.

I would use the interface when you need a free connection, for example. when the caller is a different module or is used by another developer. If you have a tight connection, like a local package implementation, I cannot work with the interface.

In the above example, you can add unit test to find out which values ​​are set, or the developer may want to use a buffer that registers which values ​​are set. Using an interface makes this easy.

+7
source

The main goal of extensibility .

At the moment, implementation 1 seems good. However, if you later decide to implement the buffer in a more efficient way, with less memory used by concurrency, you can always change this without affecting the interface or the contract that this interface provides to your other classes, or other developers in your team or others teams.

+2
source

You do not need to use the interface there, as you already know.

Using the interface has 2 big advantages. 1) It is much easier to use a different implementation of such an interface. While it is inside the details of your class, its value does not matter, but if you decide to make it externally assignable / inherit this class and change the behavior, this can be useful. 2) You can only use the method from this interface, so it can simplify the search for the correct method (some classes have many methods that do "something else"). If the class implementation has several (public) methods, and all of them are from this interface, you obviously get nothing.

And it has drawbacks: 1) you do not see the method not from the interface (if you need tham, you should not use this interface) 2) from the code using it, it is not so easy to get to the actual implementation.

by the way. You should write conventions for writing in java (class / interface names with a first letter margin, e.g. manufacturer instead of manufacturer)

0
source

All Articles