Constants in Java avoid anti-pattern methods _java

Source: Internet
Author: User
Tags introductions

In applications, we often need a constant file to store shared constants that are referenced in more than one place. In the design of 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 requirements). I have two options:

Use interfaces, such as:

Package one;
Public interface Constants {
String name= "name1";
int max_val=25;
}
or
package two;
public class Constants {public
static final String name= "name1";
public static final int max_val=25;
}

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

Also, a simple test shows that the same interface (bytecode file) occupies 209 bytes (on the Ubuntu 14.04 Machine), while the class (bytecode file) occupies 366 bytes (the same operating system). Fewer bytecode files means lower cost of loading and maintaining. 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 it loads faster.

This looks very good, but this is an example of a typical inverse pattern. Although it is helpful to use an interface to hold constants, this leaves a hole in the later extension of the application.

Suppose to exist in a class that is tightly "dependent on these constants." The developer fills in the class with a reference to the constants through the interface. Such as:

Copy Code code as follows:
PackageName. Constant.constant_name

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

However, once he implements the interface, all constants become "contracts" (since all constants are part of the public, static). This leads to the addition of unnecessary constants for this class. This would shake the whole foundation and cause confusion. There is no way in Java to block 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 private to prevent the class from being instantiated so that it never breaks the convention. In addition, if a particular constant is used more than once in the same class, the developer can use static introductions.

All for constant classes, the better design should be:

Package three;
Make the class non-extendable by adding final add final keyword to avoid inheriting public
final class Constants {
//hide the Construc Tor hidden builder
Private Constants () {} public
static String name= ' NAME ';
}

Examples of static introductions:

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 referred to as an interface constant inverse pattern (Constant Interface anti-pattern).

The above is the Java constant in the use of how to avoid the reverse mode of the solution, I hope to help you learn.

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.