[opencms-dev] just a remark on the use of Interfaces and Constants
Apostoly Guillaume
ApostolyG at mail.europcar.com
Thu Jul 24 09:52:01 CEST 2003
Hi all,
This is just to be considered as a remarks about OpenCMS code. This is
nothing dramatic, and I did that "mistake" before a lot also. It's the use
of interfaces to define constants. It's possible to make smaller code (=>
diminish memory usage) by avoiding this practice.
Below is an extract of SDN - Core Java Technologies newsletter about that.
Maybe this can give ideas for the future ...
Regards,
Guillaume.
INTERFACES AND CONSTANTS
When you want to use global constants in an application, it is often
tempting to define the constants in an interface and then implement the
interface in the class using the constants. For example, suppose you have
the following interface that contains approximations for the numbers pi and
e.
public interface TranscendentalConstants {
public static final double PI = 3.14159;
public static final double E = 2.71828;
}
Suppose too that you have a second collection of constants that contains
approximations for the square roots of two and three.
public interface IrrationalConstants {
public static final double SQRT_TWO = 1.414;
public static final double SQRT_THREE = 1.732;
}
A common strategy for using these constants is to create a class like the
following.
public class BadUseOfConstants implements
TranscendentalConstants, IrrationalConstants {
public double sinPiOverFour(){
return SQRT_TWO / 2;
}
public double sinPiOverThree(){
return SQRT_THREE / 2;
}
private void outputResults() {
System.out.println("Pi is approximately " + PI);
System.out.println(
"The sin of Pi/4 is approximately " +
sinPiOverFour());
System.out.println(
"The sin of Pi/3 is approximately " +
sinPiOverThree());
}
public static void main(String[] args) {
new BadUseOfConstants().outputResults();
}
}
Even though this code runs, it is an improper use of interfaces. That's
because BadUseOfConstants is not an extension of the type
TranscendentalConstants. Instead, BadUseOfConstants is a consumer of
TranscendentalConstants. There are two reasons why using interfaces in this
way is so attractive. First, you can easily use constants defined in two
different interfaces. If the constants were used in different classes
instead, multiple inheritance would be required to perform the same task.
Second, by implementing the interfaces, you can refer to the constants
without qualifying them as SQRT_TWO instead of as
IrrationalConstants.SQRT_TWO.
You can address this second problem of having to qualify constants by
caching the variables locally like this.
public class OKUseOfConstants {
private double PI = TranscendentalConstants.PI;
private double SQRT_TWO =
IrrationalConstants.SQRT_TWO;
private double SQRT_THREE =
IrrationalConstants.SQRT_THREE;
public double sinPiOverFour() {
return SQRT_TWO / 2;
}
public double sinPiOverThree() {
return SQRT_THREE / 2;
}
public void outputResults() {
System.out.println("Pi is approximately " + PI);
System.out.println(
"The sin of Pi/4 is approximately " +
sinPiOverFour());
System.out.println(
"The sin of Pi/3 is approximately " +
sinPiOverThree());
}
public static void main(String[] args) {
new OKUseOfConstants().outputResults();
}
}
When J2SE 1.5 is released, a new mechanism for importing specific constants
will be introduced that will address both of these issues. In the meantime,
you might want to introduce an inner class or a different class that
collects all of the interfaces containing the constants you want to use.
Here's an example. In the BetterUseOfConstants program that follows, the
inner class ConstantCollector is used to collect the constants declared in
TranscendentalConstants and IrrationalConstants.
public class BetterUseOfConstants {
private ConstantCollector constants =
new ConstantCollector();
public double sinPiOverFour(){
return constants.SQRT_TWO / 2;
}
public double sinPiOverThree(){
return constants.SQRT_THREE / 2;
}
private void outputResults() {
System.out.println(
"Pi is approximately " +
constants.PI);
System.out.println(
"The sin of Pi/4 is approximately " +
sinPiOverFour());
System.out.println(
"The sin of Pi/3 is approximately " +
sinPiOverThree());
}
public static void main(String[] args) {
new BetterUseOfConstants().outputResults();
}
private class ConstantCollector implements
TranscendentalConstants, IrrationalConstants{}
}
Running this code should produce the following results:
Pi is approximately 3.14159
The sin of Pi/4 is approximately 0.707
The sin of Pi/3 is approximately 0.866
In this case ConstantCollector is a type extension of the two interfaces.
_______________________________________
Guillaume APOSTOLY
Business-Analyst EIS-BSD
Tél: +33 (0)1.30.44.95.22
Fax: +33 (0)1.30.44.98.08
ApostolyG at mail.europcar.com <mailto:ApostolyG at mail.europcar.com>
_______________________________________
More information about the opencms-dev
mailing list