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" ?
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.
Joined: Oct 31, 2009
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 !