I’m fairly new to design patterns. What is the best pattern when a developer is using a library (in my case a library to screenscrape HTML) and doesn’t want the client/calling classes to be ‘stuck’ to that library?
For example, if I am using an class in a library called HtmlPage, and a client says ‘getPage()’ – I don’t want to return the HtmlPage Object from the library but some kind of wrapper of the HtmlPage that I can swap out if I decide to change libraries. Is it as simple as this? Or am I missing something? Would I have to do this for every object in the library?
public class HtmlPageWrapper {
private HtmlPage htmlPage;
public HtmlPageWrapper(HtmlPage) {}
public getTableOnPage() {
return htmlPage.getTableOnPage;
}
// etc...
}
Thanks!
I think you’re interested in the Adapter Pattern. This will let you keep these third party classes away from your code by creating an
Adapterclass that exposes the abstract operations your application will use.You don’t necessarily have to do this for every object in the library, just make sure you wrap the main objects which you will need to interact with.