0

I'm developing a simple application that crawls in web pages to obtain some information. For this I used and tested some libraries, like crawler4j, jsoup, jaunt and htmlunit. I exchanged several times a API to another because sometimes perceived that one served me better in certain feature than the one I was using. I may have to do it other times and every time I do so I have to go around the code making various refactorings.

So I decide to separate the calls for this APIs in a kind of encapsulated classes that holds all the operations I have to do with this API.

Is there a design pattern to mitigate this problem?

Below a simple example that I use "Handler" suffix:

EDIT the final version:

public interface CrawlerApiHandler {
    String visit (String url);
}

public class JsoupCrawlerApiHandler implements CrawlerApiHandler {
    public static final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36";

    @Override
    public String visit(String url) {
        try {
            return Jsoup.connect(url).timeout(20000)
                    .userAgent(USER_AGENT).get().toString();
        } catch (IOException e) {
            //LOG
            return "";
        }
    }
}

public class JauntApiHandlerImpl implements CrawlerApiHandler {
    UserAgent userAgent;

    public JauntApiHandlerImpl(UserAgent userAgent) {
        this.userAgent = userAgent;
    }


    @Override
    public String visit(String url) {
        try {
            return userAgent.visit(url).toString();
        } catch (ResponseException e) {
            return "";
        }
    }
}
3
  • 3
    Writing a class to encapsulate the operations you need without exposing the specific library you rely on sounds like exactly the sort of thing you should be doing. What's the problem that you think requires a design pattern?
    – Ixrec
    Commented Jan 30, 2016 at 1:20
  • I don't know if there is a design pattern to do this. If so it will be more easy to name the class than just XY Holder and to implement it more correctly.
    – alexpfx
    Commented Jan 30, 2016 at 2:10
  • The Adapter pattern might fit your use case. Commented Jan 30, 2016 at 10:05

2 Answers 2

1

As I understand your question, this is the Bridge Pattern. See https://en.m.wikipedia.org/wiki/Bridge_pattern

"The bridge pattern is a design pattern used in software engineering which is meant to "decouple an abstraction from its implementation so that the two can vary independently".[1] The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes."

1
  • That's it:Bridge pattern!
    – alexpfx
    Commented Jan 30, 2016 at 17:12
1

That's the Strategy Pattern.

If you're looking for names, I'd suggest

  • CrawlerStrategy
  • JsoupCrawlerStrategy
  • JauntCrawlerStrategy

Actually, I'd go with ICrawlerStrategy, but that's just because I'm a .Net dev. :)

1
  • Strategy pattern allows for changes at runtime. As I understand the question, he changes libraries and recompiles but wishes to have minimal code change.
    – user949300
    Commented Jan 30, 2016 at 6:36

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