Interfaces with Constants Only(java中通用常量定义)
Interfaces with Constants Only
Before the static import became available, a trick for avoiding the need to write the class name for static constants was to create an interface whose only purpose is to hold constants.
For example, say that you need the constants ALPHA and OMEGA for several different programs. The first option is to create a utility class that holds the constants. e.g.:
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
Then in your other classes, you would refer to these constants with
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
However, placing the MyConstants class name wherever these constants appear can be tedious and also makes the code less readable, especially in a formula like
x = (3.1 * MyConstants.ALPHA)/(1.0 + MyConstants.OMEGA);
An alternative is to put the constants into an interface. This will not interfere with the class design since there is no limit to the number of interfaces that a class can implement.
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
Any class that needs these constants can implement MyConstantsImpl. The methods in the program will then refer to the constants simply with ALPHA and OMEGA.
Then a method in the class that implements MyConstantsImpl could use the constants in an equation without the class reference:
x = (3.1*ALPHA)/(1.0 + OMEGA);
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
whcih is far more readable.
This technique, however, violates the object-oriented design of the language. That is, you are not really implementing anything. You would not think of a class that implements MyConstantsImpl as a MyConstants object in the sense of taking on an identifiable behavior of an interface.
So constants-only interfaces are ill advised and considered bad programming style . Use a class instead and then statically import the class.
posted on 2006-03-13 11:05 Bona shen 阅读(1321) 评论(1) 编辑 收藏 举报