R
Rhino
I'm trying to implement the advice I was given the other day about the
placement of constants that were shared in various classes of my project.
I've managed to thin out my FooConstants class considerably on that basis of
that advice but I'm having specific cases that are giving me trouble.
First, let me begin by correcting an inadvertent misstatement I made about
this project. I said that it was generating resumes in various formats and
that all of the files were being kicked off by a single class
(ResumeFileGenerator, although I didn't state its name previously). As I
look at the code again a bit more closely, I see that this is not quite the
case. A standalone class called ResumeApplet displays my resume in the form
of an applet.
Now, several of the classes that generate resumes, including ResumeApplet,
are making use of colors from a specific palette of colors that are embodied
in an enum. The enum lists several color palettes and each palette has a
color to be used for displaying text, and some contrast colors for use in
JPanels or HTML or in heading text in PDFs. I want to make sure that all of
the classes that generate Resumes use the exact same color palette to do
their jobs; I don't want the applet to use a predominantly red palette while
the HTML page uses a predominantly green palette. Every instinct I have says
that I should be establishing exactly which palette I want to use ONCE for
the project and that all classes should use that same palette.
Clearly, the color palette is not a file/path kind of information that might
best be situated in a properties file and isn't a String anyway. Does this
constitute "configuration" information? If so, where do I put it? And if
it's not configuration information, where should I put it so that all
classes in the project use that specific color palette? It's just one line
of code:
ColorPalette colorPalette = ColorPalette.BLUES;
but I'm really struggling with where it needs to be. I need to be sure that
other classes in the project don't suddenly start using ColorPalette.REDS
(or whatever) and that's the obvious risk if I have a line defining which
colorPalette I want in each class that needs colors.
I was thinking of establishing the color palette in the ResumeFileGenerator
class, probably in the constructor, and then passing a reference to each
class that needed it but this won't help me with the standalone
ResumeApplet.
--
I have a few other constants which strike me as problematic too, although
the color palette feels like the big one. Let me mention these too now in
case they have different solutions than Color Palette.
Several of the classes write the word "Resume" in one or another places and
I am trying to write it so that the acute accents on the two e's appear
correctly. I've got several constants for the different variations:
public static final String RESUME_HTML_LOWERCASE = "résumé";
public static final String RESUME_HTML_CAMELCASE = "Résumé";
public static final String RESUME_UNICODE_LOWERCASE = "r\u00e9sum\u00e9";
public static final String RESUME_UNICODE_CAMELCASE = "R\u00e9sum\u00e9";
I need the first two in HTML documents that are being generated and the
latter two in ASCII, PDF, and CSS documents that are being generated. Since
each of these is used in at least two different classes, I'm not sure where
best to place these.
All of the classes in the project share a common set of ResourceBundles. (I
want to keep open the possibility of generating resumes in foreign languages
since I have a working knowledge of two other languages and could picture
working in countries where those languages are spoken. I haven't actually
translated phrases like "Job Target" or "Format Education" to the other
languages but I _could_ and might do that.) The base name of the
ResourceBundles is used in both ResumeFileGenerator and ResumeApplet. Where
should I put its definition so that both classes use the exact same name?
Lastly, let me raise the case of what I'll call a "convenience constant". I
tend to prefer writing the name of a String constant called NEWLINE rather
than a "\n" in my statements simply because
the former seems a little clearer to me. I've used this technique in many
classes in many projects so this constant has much wider scope than just
this one project. Would it make sense to have a class of constants in my
Common project and just accumulate any similar widely used constants there?
If not, what is a better way to handle this kind of situation? Let me stress
that these "convenience constants" are not a big deal to me; if good OO
design opposes the very notion and prefers use of "\n" in all cases rather
than a constant, that's fine by me. I'm just trying to understand how a
constant that is widely used in many projects should be handled.
One (or more) of the responders to my original question said that classes
containing Constants weren't absolutely unthinkable, although there were
usually better ways. I haven't stumbled on exceptions that really DO belong
in a FooConstants class have I?
placement of constants that were shared in various classes of my project.
I've managed to thin out my FooConstants class considerably on that basis of
that advice but I'm having specific cases that are giving me trouble.
First, let me begin by correcting an inadvertent misstatement I made about
this project. I said that it was generating resumes in various formats and
that all of the files were being kicked off by a single class
(ResumeFileGenerator, although I didn't state its name previously). As I
look at the code again a bit more closely, I see that this is not quite the
case. A standalone class called ResumeApplet displays my resume in the form
of an applet.
Now, several of the classes that generate resumes, including ResumeApplet,
are making use of colors from a specific palette of colors that are embodied
in an enum. The enum lists several color palettes and each palette has a
color to be used for displaying text, and some contrast colors for use in
JPanels or HTML or in heading text in PDFs. I want to make sure that all of
the classes that generate Resumes use the exact same color palette to do
their jobs; I don't want the applet to use a predominantly red palette while
the HTML page uses a predominantly green palette. Every instinct I have says
that I should be establishing exactly which palette I want to use ONCE for
the project and that all classes should use that same palette.
Clearly, the color palette is not a file/path kind of information that might
best be situated in a properties file and isn't a String anyway. Does this
constitute "configuration" information? If so, where do I put it? And if
it's not configuration information, where should I put it so that all
classes in the project use that specific color palette? It's just one line
of code:
ColorPalette colorPalette = ColorPalette.BLUES;
but I'm really struggling with where it needs to be. I need to be sure that
other classes in the project don't suddenly start using ColorPalette.REDS
(or whatever) and that's the obvious risk if I have a line defining which
colorPalette I want in each class that needs colors.
I was thinking of establishing the color palette in the ResumeFileGenerator
class, probably in the constructor, and then passing a reference to each
class that needed it but this won't help me with the standalone
ResumeApplet.
--
I have a few other constants which strike me as problematic too, although
the color palette feels like the big one. Let me mention these too now in
case they have different solutions than Color Palette.
Several of the classes write the word "Resume" in one or another places and
I am trying to write it so that the acute accents on the two e's appear
correctly. I've got several constants for the different variations:
public static final String RESUME_HTML_LOWERCASE = "résumé";
public static final String RESUME_HTML_CAMELCASE = "Résumé";
public static final String RESUME_UNICODE_LOWERCASE = "r\u00e9sum\u00e9";
public static final String RESUME_UNICODE_CAMELCASE = "R\u00e9sum\u00e9";
I need the first two in HTML documents that are being generated and the
latter two in ASCII, PDF, and CSS documents that are being generated. Since
each of these is used in at least two different classes, I'm not sure where
best to place these.
All of the classes in the project share a common set of ResourceBundles. (I
want to keep open the possibility of generating resumes in foreign languages
since I have a working knowledge of two other languages and could picture
working in countries where those languages are spoken. I haven't actually
translated phrases like "Job Target" or "Format Education" to the other
languages but I _could_ and might do that.) The base name of the
ResourceBundles is used in both ResumeFileGenerator and ResumeApplet. Where
should I put its definition so that both classes use the exact same name?
Lastly, let me raise the case of what I'll call a "convenience constant". I
tend to prefer writing the name of a String constant called NEWLINE rather
than a "\n" in my statements simply because
the former seems a little clearer to me. I've used this technique in many
classes in many projects so this constant has much wider scope than just
this one project. Would it make sense to have a class of constants in my
Common project and just accumulate any similar widely used constants there?
If not, what is a better way to handle this kind of situation? Let me stress
that these "convenience constants" are not a big deal to me; if good OO
design opposes the very notion and prefers use of "\n" in all cases rather
than a constant, that's fine by me. I'm just trying to understand how a
constant that is widely used in many projects should be handled.
One (or more) of the responders to my original question said that classes
containing Constants weren't absolutely unthinkable, although there were
usually better ways. I haven't stumbled on exceptions that really DO belong
in a FooConstants class have I?