开发者

hidding api with interfaces in Java

Can somebody explain me what "hidding api with interfaces in Java" means ? How can I use the API functions by means of interfaces ? I would need an small working example to understand the how the interfaces hides the api non public parts, and how can I use the api puplic parts in the same time.

Thanks in advance.

THANKS GUYS FOR THE QUICK REPLY, GIVE ME TIME TO THINK OVER THE ANSWERS.

LAST BUT NOT LEAST THANKS FOR YOUR TIME AND EFFORT!

II. My second question is : What happens in the background this case below ?

IBinaryOperation op = BinaryOperationProvider.getOperation("multiply");

or 

List<String> myList = new LinkedList<String>();

Its not clear for me because the inter开发者_StackOverflow社区faces consist of methods' declarations that's why i dont understand what could happened in the lines above. Is there any meaning of the equality between empty method of used interfaces and objects ? Sorry for my weak English.


For instance, you may declare and create a list of strings as follows:

List<String> myList = new LinkedList<String>();

List is the type of myList. It is an interface. It means that all subsequent calls to methods of myList will be done through the interface: you may only call methods declared in the List interface.

However, the concrete class of the object is LinkedList, that contains more methods, some of them reflecting the structure of a linked list (for instance addFirst and addLast). But these methods are hidden because of the way you declared the variable. You chose to access the object through a given (restrictive) interface.

It may seem restrictive, but it also means that you can change your code at any time, and replace the concrete class LinkedList with any other class that implements the List interface, for example ArrayList.


Usually when you expose your API, you should hide the implementation details as much as possible and expose it via simple interfaces.

For e.g. Suppose that you give an api for adding two numbers.

Soln1 (Bad soln) Give the following class to client

public class Adder {
    public void setA() {..}

    public void setB() {..}

    public int add() { return A + B; }  
}

Soln 2 (better soln): Give the following interface to the client.

public interface Adder {
    public int add(int a, int b);

}

Now why is soln 2 a better solution. If you had given user the first soln. The client is bound to the class Adder. Suppose later you have a new implementation of addition that could add the numbers in the cloud(over-imaginative :)), you may have to as the client to change their code to use the new class.

Instead if you just give them the interface, you could provide many implementation and have a factory mechanism to choose the suitable implementation.


Here's a very simple example that uses an interface:

public interface IBinaryOperation {
    public int performOp(int a, int b);
}

private class MultiplicationProvider implements IBinaryOperation {
    public int performOp(int a, int b) {
        return a * b;
    }
}

public class BinaryOperationProvider {
    static IBinaryOperation getOperation(String name) {
        if ("multiply".equals(name)) {
            return new MultiplicationProvider();
        } else if ("add".equals("name)) {
            return new AdditionProvider();
        } // ...
    }
}

You would use this like:

IBinaryOperation op = BinaryOperationProvider.getOperation("multiply");
int c = op.performOp(a, b);

In the above example, MultiplicationProvider is completely private to the implementation of the API. The only public part is the IBinaryOperation interface, and the BinaryOperationProvider class.


Just to be clear, what's "hidden" is not the API, but the implementation. Most clients of List (to use an example above) don't need to know which kind of List is actually being used. (Just like most TCP/IP clients don't need to know anything in particular about the network they're using -- just that it supports the standard connection abstraction.)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜