简体   繁体   中英

How can I wrap a library to allow me to switch it out more easily?

The title basically says it all. I am using Jsoup to scrape Hackernews, and then I'm making a restful api to return json of the scraped info.

I've been told that wrapping the libraries you use is a good idea. So I'm wondering how can I do that? For example using Jsoup I am given a 'Document' back that holds the html. This type bleeds through to every function I'm using, basically. I select and element from that 'Document' and I'm given 'Elements' back, and then that bleeds through.

How would I go about wrapping a library and manage the types that come along with that library?

I hope this is enough information. If you have any questions, or need more info let me know.

Applying a structural design pattern such as the Adapter pattern would suit you best. Adapters are meant to join two incompatible pieces of software to each other. They are a suitable means to encapsulate an API and translate back and forth to a form that is more natural or intuitive to your own software. Be sure to reflect upon how the API you are using may change in the future so you can design your Adapter with this in mind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM