Programatically grouping and typehinting different classes

Question

Given I have a class that uses some kind of searcher to get and display a list of URLs, like this:

package com.acme.displayer;

import com.acme.searcher.SearcherInterface;

class AcmeDisplayer {

    private SearcherInterface searcher;

    public AcmeDisplayer(SearcherInterface searcher) {
        this.searcher = searcher;
    }

    public void display() {
        List<String> urls = searcher.getUrls();

        for (String url : urls) {
            System.out.println(url);
        }
    }
}

Whereas the SearcherInterface looks like the following:

package ch.acme.searcher;

public interface SearcherInterface {
    List<String> getUrls();
}

There's multiple implementations of these searchers. (One, for instance, only returns a hardcoded list of Strings for testing purposes).

Another one, however, performs HTTP Requests to whatever API and parses the response for URLs, like so:

package com.acme.searcher.http;

import com.acme.searcher.SearcherInterface;

public class HttpSearcher implements SearcherInterface {
    private RequestPerformerInterface requestPerformer;
    private ParserInterface parser;

    public HttpSearcher(RequestPerformerInterface requestPerformer, ParserInterface parser) {
        this.requestPerformer = requestPerformer;
        this.parser = parser;
    }

    List<String> getUrls() {
        InputStream stream = requestPerformer.performRequest();

        return parser.parse(stream);
    }
}

The splitting of such an HTTP request is done because of seperation of concerns.

However, this is leading to a problem: A Parser might only be built for a certain API, which is represented by a certain RequestPerformer. So they need to be compatible. I've fiddled around with generic types for such a structure now, i.e. having a TypeInterface that both arguments of HttpSearchers constructor should implement, but I didn't get it working... Another approach would be to just implement a check in one class if the other one is compatible with it, but that seems ugly.

Is there any way to achieve such a grouping of RequestPerformers and Parsers by the API they're handling? Or is there something wrong with the architecture itself?


Show source
| generics   | java   | oop   | architecture   | software-design   2017-01-06 22:01 1 Answers

Answers ( 1 )

  1. 2017-01-08 17:01

    Your HttpSearcher seems like such a device to group these 2 together. You could create a factory class that returns HttpSearcher and other classes like it, and code that factory to group the compatible RequestPerformers and Parsers together.

    The reason why I wouldn't advice leveraging the type system, e.g. through generics, is that the type InputStream can guarantee nothing about the format/type of data it holds. Separating the responsibility of getting the raw data, and parsing seems like a good idea, but you will still have to 'manually' group the compatible types together, because only you know what format/type of data the InputStream will hold.

◀ Go back