How constants in Java avoid anti-patterns

Source: Internet
Author: User

In applications, we often need a constant file to store shared constants that are referenced in multiple places. In designing the application, I also encountered a similar situation, many places need a variety of constants.

I'm sure I need a separate file to store these static public constants. But I'm not particularly sure if I should use an interface or a class (enumerations don't meet my needs). I have two options:

Use interfaces, such as:

1

2

3

4

5

packageone;

publicinterfaceConstants {

String NAME="name1";

intMAX_VAL=25;

}

Or

1

2

3

4

5

package  two;

public  class   constants {

public  static  final  string name= " name1 " ;

public  static  final  int  max_val= 25

}

My point of view is to use interfaces. Because the interface automatically sets the member variable to static (static), immutable (final), this prevents some cases from mistakenly adding new constants. This also makes the code look simpler and clearer.

At the same time, a simple test shows that the same interface (bytecode file) occupies 209 bytes (on Ubuntu 14.04 machines), while the class (bytecode file) occupies 366 bytes (the same operating system). Fewer bytecode files mean less cost to load and maintain. In addition, when the JVM loads the interface, it does not need to worry about the extra features provided by the class (such as overloading, dynamic binding of methods, etc.), so loading is faster.

This looks very good, but this is an example of a typical anti-pattern. While it can be helpful to use interfaces to save constants, this leaves a loophole for late application extensions.

Suppose to exist in a class, tightly "depends on these constants." The developer fills the class with a reference to the constant through the interface. Such as:

1

packagename.Constant.CONSTANT_NAME

So, to "clean up" the code, he might want to implement the interface so he doesn't have to write "PackageName." Constants ", all constants can be accessed directly.

However, once he implements the interface, all constants become part of the "contract" (because all constants are public, static). This leads to the addition of unnecessary constants for this class. This will shake the whole foundation and cause confusion. There is no way in Java to prevent a class from implementing an interface.

In another way, we can set the class to final so that it cannot be extended. Even, we can set the constructor to be private to prevent instantiation of this class, which will never break the contract. In addition, if a particular constant is used more than once in the same class, the developer can use static ingestion.

For all constant classes, a better design should be:

1

2

3

4

5

6

7

packagethree;

//make the class non-extendable by adding final 增加final关键字来避免继承

publicfinal classConstants {

//Hide the constructor 隐藏构造器

privateConstants(){}

publicstaticString NAME="name";

}

Examples of static introductions:

1

2

3

4

5

6

import  static  three. Constants.name;

public  class   useconstants {

   public   static  void   main (string[] args) {

       System.out.println ( "The value of constants is" + NAME);

   }

}

This design problem is also known as interface constant anti-pattern (Constant Interface anti-pattern).

How constants in Java avoid anti-patterns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.