Re: Desing Question re Multiple Constructors



"Rhino" wrote:
>I have a design question about constructors.
>
....
> I've just finished writing a class that creates a JComponent which
> displays
> images: static and animated GIFs, JPGs, and PNGs. The images can originate
> anywhere:
> - in a standalone file on the file system, e.g. file:d:\\myImages\\foo.gif
> - in a standalone file online, e.g. http://shazam.com/images/bar.gif
> - in a jar on the filesystem, e.g.
> jar:file:d:\\myJars\\test.jar!/Images/baz.gif
> - in a jar that is online, e.g.
> jar:http://shazam.com/images/test.jar!/Images.baz.gif
> - in a jar that is in the application's classpath, e.g.
> jar:file!/Images/baz.gif
>
> The sources of the images are identified, initially, as Strings, as you
> can
> see in the examples above. In effect, I am using three standard protocols
> to
> describe the locations:
> - file: (for standalone files on the filesystem)
> - http: (for standalone files online)
> - jar: (for images found in jars, regardless of whether they are online,
> in
> the filesystem, or in the classpath)
>
[big snip]

Hi,

Well, let me look at this as if I was a potential user of your class.

You have a constructor with a String. You analyse the String to determine
how the file must be loaded. This kind of constructor would be practical if
it could be used intuitively. But it's not. Firstly there is no way to know
what protocol can be used (but by looking at the javadocs of the constructor
(up to date?), which will become unpractical if there are too many
protocols). Secondly the user must use a specific construction for the
different protocols. I don't know the jar protocol and I see that there is a
!-token somewhere in it. For an other protocol there could be a $ or a &
somewhere, as a user I don't want to be bothered with such things.

You've added other constructors with FileLocation, JarLocation,
OnlineLocation as argument. Making different classes for the different
location types is better than using a String, but make sure these Location
objects can be created in a user friendly way. There is no need for
different constructors. Make a Location class or interface, make one
constructor with a Location argument and let the FileLocation, JarLocation,
OnlineLocation classes extends / implements the Location class / interface.


Mike


.



Relevant Pages

  • Re: Desing Question re Multiple Constructors
    ... >> images: static and animated GIFs, JPGs, and PNGs. ... > You have a constructor with a String. ... This kind of constructor would be practical ... > protocols). ...
    (comp.lang.java.help)
  • Dynamic loading of .class files and deserializing of object
    ... There is a client and server nodes working together. ... transfers a .class (or .jar) file to the client with the serialized ... Constructor constrOis = null; ...
    (comp.lang.java.programmer)
  • Re: Desing Question re Multiple Constructors
    ... Your problem description makes me wonder why you don't provide a constructor ... > images: static and animated GIFs, JPGs, and PNGs. ... > - in a jar that is online, ... > URL depending on the protocol used? ...
    (comp.lang.java.help)
  • Re: [PHP] variables in CSS in PHP question/problems
    ... In PHP 4 your constructor has to have the same name as the class ... // we start off with the path to your images directory ... level with the index file and css, ...
    (php.general)
  • Image loading
    ... public class Main extends java.applet.Applet ... public class Room extends java.applet.Applet ... I have this working _provided_ the images are loaded through Room's ... are loaded through Room's constructor. ...
    (comp.lang.java.programmer)