4

I'm reading Robert Sedgewick's Algorithms book and I often see the term API client. Please take a look at this exercise:

Write an iterable Stack client that has a static method copy() that takes a stack of strings as argument and returns a copy of the stack. Note : This ability is a prime example of the value of having an iterator, because it allows development of such functionality without changing the basic API.

I'm confused as to whether he wants me to implement the Stack API by creating a concrete class that implements Stack or he wants me to write a program that uses the Stack API functionality. So does he want this

class StackImpl implements Stack<Item>{
}

or this?

public static void main(String[] args){
    Stack<String> stack = new Stack<String>();
    stack.push("to");
    stack.push("be");
    stack.push("or");
    stack.push("not");
    System.out.println(copy(stack));
}

public static Stack<String> copy(Stack<String> stack){
     //copy from stack to a new stack and return
}
  • 10
    A client of X is always a component that uses X, so it's the second alternative. – Kilian Foth Nov 2 '17 at 7:34
2

When you have an API, the client is any code that makes use of the functionality offered by the API.

I haven't read the book, but:

  • the second option seems definitively to comply with the exercise: you have some code that implements a copy() that relies on the use of API functions to perform its duties, and on top of that you provide some demo code that shows how to use copy() with the API.
  • the first option considers Stack to be an interface. This would mean that there is no Stack implementation, but that on contrary, you have to provide the API. So you would make a provider/an implementation of the API and not a client.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.