JFreechart
0.9.21
Source analysis report
Author: Beijing gambling Schumacher
Report 1: Analysis why use Compable and Number interface
Method classes are used to use the Import Java.lang.Comparable class and java.lang.number class, mainly
Digital Long, Btye, Float, Short, Double, Integer inherited the Number class, implemented Comparable interface, UML class diagram in J2SE.
Figure 1.0
All values and values are used using Comparable and Number classes.
(1) Analysis of the assignment operation.
The example source code is as follows:
Private number value = null;
Public Void SetValue (Final Number value) {
THIS.VALUE = VALUE;
}
Using the Comparable and Number class, you can enter any number of digital types.
(2) Analyze the value operation.
The example source code is as follows:
Public Number getValue (Final Comparable Key) {
// Take the value. . . . .
Return this. Value;
}
Using the Comparable and Number class, you can enter any number of digital types.
(3) Polymorphization value use operation.
The example source code is as follows:
Comparable key = (comparable) orerator1.next ();
Number n = this.dataset.getValue (key);
IF (n! = null)
Double v = n.doubleValue (); // Note that this place uses type conversion, implementing downward transformation
IF (Key! = null)
String K = key.toString (); // Note that this place uses the Object class to convert.
Number finally used a Double type.
Comparable is ultimately used as a String type.
Summary: The above operation is flexible to use the interface Comparable and abstract class Number so that the operation is convenient for the user to enter the operation, which can adapt to the user's input type, and finally the transformation is completed by the program. From the user's perspective, it is considered to enter any digital type, program Will not go wrong. From a programming point of view, you can only write only a Number object and a Comparable object, you can arbitrarily assign a value, do not need to redefine the type of object, and the Number object is ultimately used is the Double type Comparable object. The String type is directly implemented by the polymorphism when needed to transition down.
Report 2: Using Final Keywords in Method Parameters
Final Keyword How to use:
For basic types of variables, there is no practical meaning because the basic type of variable is transmitted when the method is called, that is, you can change this parameter variable in the method without affecting the calling statement, however Object variables are very practical because object variables are passed to deliver them, so that you can modify the object variable in the method also affect object variables in the call statement, when you don't need to change as a parameter in the method When the object variable, it is clearly used for the declaration to prevent you from being unintentionally modified to affect the calling method.
Use Final to make Data or Object Reference into an invariant (constant), use Final. Note that Final only makes Object Reference itself does not variable, and does not limit the change in object it refers. In the method parameters, use the final keyword. E.g:
Public Number getValue (Final Comparable Key) {
Number result = null;
Final Int index = getIndex (key);
IF (INDEX> = 0) {
Result = getValue (index);
}
Return Result;
}
Analysis: A large number of PUBLIC NUMBER GETVALUE (Final Comparable Key) final keywords in the source code. When the method is not required to change the object variable as a parameter, it is clear that the declaration of FINAL will prevent unintentional modifications.
Summary: In the method, the flexible use of final, flexible use of Final improved the speed, which also clarified the variables that could not be modified, but also embodied the strong internal gathering of the program, because Final appears inside the method, so there will be a class Members declared to Final, which is difficult to modify the situation of the class, which is very in line with logic.
Report 3: How to adapt to the number of input arbitrary types
Data sets used in JFreechart, when it is worth assay to data collection, you will find that you can enter any type of number, I have implemented any input numbers for JFreeChat, which can achieve any form of polymorphism, but in the end I found It turned out to use the NumberFormat class format method. This method can be used to convert any type, converted into a string, which looks from the surface, very similar to any digital type.
Here I wrote a simple example, and the implementation of this problem:
Function: Enter any type of ability, the program is input to the number type, implements the corresponding conversion, and finally enter the String type.
Package mypack;
Import java.lang.number;
Import java.lang.comparable;
Import java.util.arraylist;
Import java.text.numberformat;
/ * Example Implemented users to enter the digital type, convert to the corresponding string
For example: Enter int 22 Result: 22
Enter Float
22f
Result: 22
Enter double 22.22 Results: 22.22
Enter double
22.22L
Result: 22.22
* /
Public class matchanynumber {
Private arraylist list = null;
Private numberFormat NumberFormat = NULL;
Public matchanynumber () {
List = new arraylist ();
Numberformat = NumberFormat.getNumberinstance ();
}
Public void setInfo () {
Try {
// Test INT number
Numberinfo ni1 = new numberinfo ();
Ni1.setname ("int"); Ni1.SetValue (22);
List.Add (NI1);
// Test Float type number
Numberinfo ni2 = new numberinfo ();
ni2.setname ("float");
Ni2.SetValue
22.22F
);
List.add (ni2);
// Test Double Digital
Numberinfo ni3 = new numberinfo ();
ni3.setname ("double");
Ni3.SetValue (22.22d);
List.add (ni3);
// Test the LONG type number
Numberinfo ni4 = new numberinfo ();
Ni4.setname ("long");
Ni4.SetValue
22L
);
List.add (ni4);
}
Catch (Exception E) {
E.PrintStackTrace ();
}
}
Public void getInfo () {
Java.util.iterator it = list.iterator ();
While (ore.hasnext ()) {
NumberInfo ni = (NumberInfo) iter.next ();
String testname = ni.getname (). TOSTRING ();
String TestValue = NumberFormat.Format (ni.getvalue ());
System.out.println ("Input Number Type:" Testname "/ Value:" Ni.GetValue ());
System.out.println ("Out Number Type:" Testname "/ Value:" TestValue);
}
}
PRIVATE CLASS NUMBERINFO {
Private comparable name = NULL;
Private number value = null;
Public void setname (Final Comparable Name) {
THIS.NAME = Name;
}
Public comparable getname () {
Return this.name;
}
Public Void SetValue (Final Number Value) {
THIS.VALUE = VALUE;
}
Public void setvalue (femive double value) {
SetValue (New Double (Value));
}
Public Number getValue () {
Return this.Value;
}
}
Public static void main (String [] args) {
Matchanynumber test = new matchanynumber ();
Test.setInfo ();
Test.getinfo ();
}
}
Output results:
Input Number Type: Int / Value: 22.0
Out Number Type: INT / VALUE: 22
Input Number Type: float / value: 22.21999313354492
Out Number Type: Float / Value: 22.22
Input Number Type: Double / Value: 22.22
Out Number Type: Double / Value: 22.22
Input Number Type: Long / Value: 22.0
Out Number Type: long / value: 22
Conclusion: Programming for Number interface, plus digital type conversion, implements data collection, can enter any number of digital types.
Report 4: How to program interface programming
There are many people on the Internet to talk about interface programming, but they are not very detailed, telling the value of the truly interface.
After analyzing JFreeChart, it can be clearly obtained, and the characteristics of interface programming.
For example: Drawing round pie chart in JFeechart, the definition of the data collection is as follows:
// Piedataset is an interface
// Defaultpiedataset is a physical class that implements a Piedataset interface.
Private Piedataset CreateDataSet () {
DEFAULTPIEDATASET DATASET = New Defaultpiedataset ();
Dataset.SetValue ("One", 10;
Return DataSet;
}
// Subsequent operation is using a PiedataSet object
Analysis: When you see the data set instantiation, use the defaultpiedataset class, but return is an interface Piedataset. When passing and calling, you use the interface PiedataSet object, but when the operation, the object is actually Calling the entity class's default page, in the JFreeChart source code, if you want to use a list, then pass the list, if you want to use numbers, then pass Number, if you want to use String, then transfer Comparable, these are interface-oriented programming Reflection.
Advantages: Programming for interfaces and abstractions, can make the program how flexible, use interface and abstract classes during the program delivery object, if you implement the entity class of these interfaces, modify changes, you can directly modify these Entity class, other calls that call these programs can not be modified, or very small amounts of modifications, so that strong inner coupling is achieved.
Disadvantages: The readable performance of the program is reduced, and it is difficult to understand the program structure for primary programmers.
Conclusion: Interface and abstract class programming is driven by major events, and people who are right. Some people may say that they don't necessarily, but from actual programming experience, this is the way the programmer will pass, this programming is suitable, very Strong programming team, such written programs, can adapt to user demand changes, relatively small, but require the programmer itself.
This article cannot be reproduced without the author's consent. If you have any questions, please send an e-mail: llhdf@yahoo.com.cn