Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design Pattern for Iterator with Pagination with Java

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.

like image 922
Rahul Avatar asked Jan 09 '17 06:01

Rahul


People also ask

What is the design pattern followed by iterator Java?

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.

How pagination is handled in Java?

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.

Which type of design pattern is the iterator pattern?

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.).

Is iterator a design pattern?

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.


1 Answers

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.

like image 127
OldCurmudgeon Avatar answered Sep 23 '22 15:09

OldCurmudgeon