Re: Desing Question re Multiple Constructors
- From: "andreas kinell" <news@xxxxxxxxx>
- Date: Sat, 29 Oct 2005 22:12:30 +0200
I think you answer your question yourself.
Since you would have to do a workaround because your constructors have the
same signature, it can't be a good design choice.
Your problem description makes me wonder why you don't provide a constructor
for Files and one for URLs. It appears to me that the constructing class
(the class calling the constructor) already knows what type of location is
being used.
andreas
"Rhino" <no.offline.contact.please@xxxxxxxxxx> schrieb im Newsbeitrag
news:HiM8f.8429$ki7.699077@xxxxxxxxxxxxxxxxxxxxxxxx
>I have a design question about constructors.
>
> I've been coding in Java for a few years now but my theory is still pretty
> shaky in some respects so I hope someone here can help me reason some
> things
> out. Rather than talking in abstractions, I'm going to give you a clear
> and
> relatively brief "real life" case of my problem.
>
> 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)
>
> The code to construct an instance of my image viewer is exactly the same
> for
> all sources of the images, except for one thing: the logic involved in
> creating a URL for the image. That logic differs depending on whether I
> use
> the 'jar:', 'file:', or 'http:' protocol. That's only a few lines of code
> but it has me wondering:
>
> Should I have a single constructor that just uses if/else to construct the
> URL depending on the protocol used?
>
> -OR-
>
> Should I have one constructor for each of the protocols, i.e. one
> constructor for images that are described with the 'jar:' protocol, one
> constructor for images that are described with the 'file:' protocol, and
> one
> constructor for images that are described with the 'http:' protocol?
>
> In a nutshell, I am wondering whether a not-terribly-complex if/else
> structure in a constructor is enough to justify creating separate
> constructors so that the if/else can be avoided.
>
> One further critical (?) factor: if I use multiple constructors, the
> inputs
> to each constructor will be identical in terms of their types: the String
> describing the source of the image, a couple of booleans, a Locale, and a
> Logger. Therefore, I will have to create three new Classes:
> - JarLocation to represent images that use a 'jar:' protocol
> - FileLocation to represent images that use a 'file:' protocol
> - OnlineLocation to represent images that use 'http:' protocol
> Then, the String that describes the source of the image gets replaced by
> JarLocation or FileLocation or OnlineLocation, depending on which
> constructor it is. That's not very difficult but I wonder if it is a Bad
> Thing because it is causing proliferation of classes (and constructors)
> due
> to unimportant distinctions or whether it is a Good Thing because it makes
> important distinctions between the new classes.
>
> To generalize the whole issue a bit, can anyone suggest some general
> guidelines on when multiple constructors should be used and why, as well
> as
> when single constructors should be used and why?
>
> ---
>
> By the way, for what it's worth, the current version of my class contains
> four constructors:
> - one that represents the location of the image via a String and uses
> if/else to create the URL
> - one that represents the location of the image via a FileLocation and has
> its own unique logic to build the appropriate URL
> - one that represents the location of the image via a JarLocation and has
> its own unique logic to build the appropriate URL
> - one that represents the location of the image via a OnlineLocation and
> has
> its own unique logic to build the appropriate URL
> All four constructors work perfectly in my unit tests. When I've received
> your answers, it will be a simple matter to delete the constructor(s) that
> I
> shouldn't have written in the first place ;-)
>
> --
> Rhino
> ---
> rhino1 AT sympatico DOT ca
> "There are two ways of constructing a software design. One way is to make
> it
> so simple that there are obviously no deficiencies. And the other way is
> to
> make it so complicated that there are no obvious deficiencies." - C.A.R.
> Hoare
>
>
.
- Prev by Date: lost
- Next by Date: Re: network is hindering my applet
- Previous by thread: lost
- Next by thread: Re: Desing Question re Multiple Constructors
- Index(es):
Relevant Pages
|
|