开发者

Requirement of Java Interface

I have two programs.

First one is without 'Buffer' interface and second is with 'Buffer' interface.

Both programs give me same output.

My question is.

why I need to create "Buffer" interface while I can get same output without interface?

Here is my both 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();

    }
}


With an interface you can use other implementations later without changing all your code. E.g. if you use a List interface, this could be a LinkedList, an ArrayList, a CopyOnWriteArrayList or something else.

You are making it clear, the details of the implementation are not important.

On the other hand, I wouldn't go around making an interface for every class. In most cases adding an interface later in the unlikely event you really need it isn't always a difficult one.

I would use an interface when you are want loose coupling e.g. when the caller is another module or used by another developer. If you have tight coupling like a package local implementation, I might not bother with an interface.

In the above example, you might want to add a unit test to see what values are set or a developer might want to use a Buffer which logs which values are set. Using an interface makes this easier.


The primary purpose is extensibility.

For now Implementation 1 seems good. However, if you later decide to implement buffer in a more efficient way, with less memory usage that handles concurrency, you can always change that without affecting the interface or the contract which this interface provides to other classes of yours, OR other developers in your team OR other teams.


You don't exactly need to use interface there, as you already know.

Using interface have 2 big advantages. 1) It is much easier to use different implementation of such interface. As long as it is inside detail of your class, its not significant, but if you decide to make it assignable from outside / inherit this class and change behavoir, it can be usefull. 2) You can only use method from that interface, so it can simplify finding the right method (SOME classes have lot of methods that do "something else"). If the implementation is class have few (public) methods and they are all from that interface, you obviously gain nothing.

And it have disadvantages: 1) you dont see method not from the interface (if you need tham, you should not use this interface) 2) from code using it, its not so easy to get to actual implementation.

btw. You should write acording conventions in java (class/interface names with first letter capital, eg. Producer instead of producer)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜