I'm attempting to make service calls to an API that returns back results in a paged format and want suggestions on the design pattern for the iterator for this.
What I have till now is something like this
public class CustomIterator implements Iterator<Type> {
private List<Result> results;
private Service service;
private int index;
private int paginatedResultSize;
private int totalResultsSize;
public CustomIterator(Service service) {
this.service = service;
this.index = 0;
this.results = getResults(index);
this.totalResultsSize = this.results.totalResultsSize();
}
@Override
public boolean hasNext() {
if (index < totalResultsSize)
return true;
return false;
}
@Override
public Type next() {
if(index == paginatedResultSize) {
getResults(index);
}
return results[index++];
}
private List<Result> getResults(index) throws Exception {
this.results = service.makeServiceCall(index);
this.paginatedResultSize = this.results.size();
return this.results;
}
}
Now, I understand that the purpose of the iterator is normally to just iterate, but I also want to encapsulate the whole pagination into a separate area so my client class can just call .next() on the class and get all values without having to know about the internal pagination details. Is there a clean pattern to getting this done?
With this the first issue I ran into was that the service call was throwing a checked exception and next() obviously does not.
Some options I noticed on the internet were to have this throw a RunTimeException which I'd rather only do as a last resort since I like checked exceptions for service calls. My gut feeling is that the service call should be done in a separate layer completely, but I'm not sure how the iterator will work with that pagination there. Any suggestions/links appreciated.
Iterator pattern is very commonly used design pattern in Java and . Net programming environment. This pattern is used to get a way to access the elements of a collection object in sequential manner without any need to know its underlying representation. Iterator pattern falls under behavioral pattern category.
Pagination in JavaIt allows users to display a part of records only. Loading all records on a single page may take time, so it is always recommended to created pagination. In java, we can develop pagination examples easily. In this pagination example, we are using the MySQL database to fetch records.
Iterator is known to be a behavioral design pattern that allows you to traverse components of a set without revealing the representation underneath (for example: stack, lists, tree, etc.).
Iterator Pattern is a relatively simple and frequently used design pattern. There are a lot of data structures/collections available in every language. Each collection must provide an iterator that lets it iterate through its objects.
This is a perfectly acceptable approach.
How you handle exceptions is up to you. If the exception is something like AttemptToReadBeyondLimit
then just return false
from hasNext
. If it is something like CommunicationsException
then by all means throw a RuntimeException
.
You shouldnt add extra layers unnecessarily.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With