It's not a secret anymore!
The moose likes Beginning Java and the fly likes Design issue for simple Downloader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Design issue for simple Downloader" Watch "Design issue for simple Downloader" New topic

Design issue for simple Downloader

Tim McMurry

Joined: Oct 31, 2009
Posts: 22

I have completed an exercise where one had to construct a simple class wich given a hostname, a portnumber and a filename would
download the file. This was to be done using only Sockets, so no use of eg. URLConnection.

I often get beat for putting too much in a single class, so now afterwards I am redoing the exercise trying to create some class-overload in a sensible way.

I have made a class HTTPRequest which wich holds info about the target to download: Hostname and path to target. The only
non-trivial method in this class is a method that writes an HTTP header to a given BufferedWriter returning the BufferedWriter after having written the 2 first
standard HTTP header request lines to it.

Now I cant really decide wether the Socket used should go inside the HTTPRequest class. My first thought was that it would make sense to have a Downloader class
which created the socket from a user-given hostname and port (which defaults to 80) which also created the appropriate HTTPRequest which would then write
the request to the OutputStream gotten from the Socket. But this looks a bit strange "conceptually" since now the hostname "sits" in two places, the HTTPRequest and in the Downloader. So where do I go wrong with my "modelling" ?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46375
One suggestion:

You pass the HTTPRequest object to the constructor of the Downloader class; then you use its getHTTPAddress() method whenever you need the address in the downloader.

Doubtless other people will have other suggestions, probably better than mine.
Tim McMurry

Joined: Oct 31, 2009
Posts: 22
I like this suggestion.

Adding more functionality to the Downloader it seems that everything would be more elegant using URLRequest. Strange requirement in the exercise to disallow this, basically you have to design a class thats already existing and in better design then one could do for now !
I agree. Here's the link:
subject: Design issue for simple Downloader
It's not a secret anymore!