[opencms-dev] just a remark on the use of Interfaces and Constants

M Butcher mbutcher at grcomputing.net
Thu Jul 24 17:26:01 CEST 2003


That is an interesting article. I used to do things in a way similar to
what they suggest (That is, defining Constants in a separate class, or
simply declaring them as public in the class I was using) until I read
an article a few years ago at jGuru suggesting that the appropriate way
of storing constants was in an interface. It cited convenient
namespacing as the primary reason, thought it had others.

It looks like jGuru is now a pay-only service, and I'm not a member. If
anyone else is, I'd be curious to hear if that article is still there
and what it's main arguments are.

>From an OO design perspective, I can definitely see why using an
interface for variables, and then "implementing" the interface to get
the variables inside the immediate namespace. I mean, it's not like I'm
ever going to cast MyObject into an I_MyConstants! Or have a method that
returns an I_MyConstants object.

Interesting reading. Thanks.

Matt

On Thu, 2003-07-24 at 01:34, Apostoly Guillaume wrote:
> 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>  
> _______________________________________ 
> 
> 
> 
> 
> 
> 
> 
>  
> _______________________________________________
> This mail is send to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://mail.opencms.org/mailman/listinfo/opencms-dev
-- 
M Butcher <mbutcher at grcomputing.net>



More information about the opencms-dev mailing list