jeudi 28 janvier 2016

How to represent web pages in OOP

Let's say there's a web page: shop.com
Basic structure:
shop.com/search
shop.com/user

shop.com/user/cart/ needs authentication session.

I am building a parser for this web page using Jsoup, to extract piece of text from a target page. I would like to program it as a "web page as an API".
So my queston is, how should I design my program?

Right now I have something like this:
Shop shop = new Shop(username, login) // instantiating the user session
Document shoppingCart = shop.getShoppingCart(); // returns org.jsoup.nodes.Document

The problem here is that what if I need more data from many different pages, e.x.:
shop.findBestDeals() // would not need login session
shop.getUsedSearchTerms() // would need login session
All the parsing weight would be on Shop class. Plus logging the user in for no reason, if page doesn't need it.
So I thought of using static methods like this, where each page would be a class:
Document cart = Cart.getShoppingCart(usersession)
Document deals = Shop.findBestDeals()
Document dealsForUser = Shop.findBestDeals(usersession) // if web page would display special items for logged in users

So basically, have web pages as classes, and they would all have static methods for data retrieval. As I now think about it, each "page-class" would require lots of (the same) imports (as other classes), would it be okay, or is there a design pattern to prevent this from happening?
This is the best I can come up with as a first year student for "web page as an API" problem.
English is not my first language, so feel free to ask further questions! :)

PS: not a homework, my personal project!

Aucun commentaire:

Enregistrer un commentaire