Java to instantiate the action of the class, you are still the same as the new corresponding object?
The number of projects handled, the code will naturally increase the amount of writing, will gradually create a sense of design patterns.
How to make a written class instantiation action, cohesion well-structured classes poly, low coupling, but also a certain expansion ability?
This article attempts to start from a few pieces of live code, to show you a different Java instantiation class.
The following code is taken from com.google.zxing source implementation:
Public Bitmatrix Encode (String contents, barcodeformat format, int width, int height, map<encodehinttype,?> hints)
Throws Writerexception {Object writer;
Switch (format.ordinal ()) {Case 1:writer = new Aztecwriter ();
Break
Case 2:writer = new Codabarwriter ();
Break
Case 3:writer = new Code39writer ();
Break Case 4:case 10:case 13:case 14:default:throw new IllegalArgumentException ("No encoder available
For format "+ format);
Case 5:writer = new Code128writer ();
Break
Case 6:writer = new Datamatrixwriter ();
Break
Case 7:writer = new Ean8writer ();
Break
Case 8:writer = new Ean13writer ();
Break
Case 9:writer = new Itfwriter ();
Break
Case 11:writer = new Pdf417writer ();
Break
Case 12:writer = new Qrcodewriter ();
Break
Case 15:writer = new Upcawriter (); Break
Case 16:writer = new Upcewriter ();
Return ((Writer) Writer). Encode (contents, format, width, height, hints);
}
One of the Barcodeformat is this:
public enum Barcodeformat {
AZTEC,
Codabar,
code_39,
code_93,
code_128,
Data_matrix,
Ean_8,
ean_13,
ITF,
maxicode,
pdf_417, Qr_code, rss_14, rss_expanded, Upc_a,
upc_e,
upc_ean_extension;
Private Barcodeformat () {
}
}
SOURCE provides the function of the information through several different types of barcode Wirter output to the bit matrix, and then output to the picture above, forming a wide range of various types of barcodes.
Bitmatrix Bitmatrix = new Multiformatwriter (). Encode (_text, Barcodeformat.qr_code, Qrcodewidth, qrcodeheight, hints);
Matrixtoimagewriter.writetofile (Bitmatrix, Qrcodeformat, qrcodefile);
The source author uses the new feature Enum enumeration class introduced in JDK 1.5 to write the Barcodeformat class, which defines the properties of different types of barcodes.
Invoke Multiformatwriter.encode () to instantiate a specific class based on the ordinal number of the entry parameter barcodeformat.xx in the enumeration class.
Switch (format.ordinal ()) {case
1:
writer = new Aztecwriter ();
break;
Case 2:
writer = new Codabarwriter ();
break;
Case 3:
writer = new Code39writer ();
break;
...............
These barcode Writer classes are implemented with two encode () methods of abstract interface Writer.
Public interface Writer {
Bitmatrix encode (String var1, Barcodeformat var2, int var3, int var4) throws n;
Bitmatrix encode (String var1, Barcodeformat var2, int var3, int var4, Map<encodehinttype,?> var5) throws Writerexce ption;
}
The specific barcode Wirter class has different logic according to different types of barcode rules.
Users do not need to pay too much attention to the implementation of the internal, the need to produce what the appearance of the bar code, the selection of appropriate bar code type can be, the author of the above examples of the implementation of the two-dimensional code.
To see the classic MVC framework WebWork a method code for dynamically instantiating classes:
private static Configuration getdefaultconfiguration () {
if (Defaultimpl = null) {
Defaultimpl = new defaultconf Iguration ();
try {
String className = getString ("webwork.configuration");
if (!classname.equals (Defaultimpl.getclass (). GetName ())) {
try {
Defaultimpl = (Configuration) Objectfactory.getobjectfactory (). Buildbean (Thread.CurrentThread (). Getcontextclassloader (). LoadClass (ClassName) );
} catch (Exception e) {
log.error ("Could not instantiate configuration", e);
}
return defaultimpl;
} catch (IllegalArgumentException localillegalargumentexception) {
}
}
}
Source code from Webwork-core, may be a lot of onlookers master did not hear WebWork, but to Struts should be thunderclap piercing, Struts2 core rewrite from WebWork.
The functionality provided by the above source code reads the class for the instantiated user-defined configuration file, which is in the configuration file.
The source author uses the class loader in the Thread.CurrentThread (). Getcontextclassloader (). LoadClass (ClassName) thread to dynamically instantiate a custom profile read class. is one of the most efficient practices.
Delegate chain for Class loader: Systemclassloader-> extensionclassloader-> bootstrapclassloader
The ClassLoader on the left of the delegation chain can naturally use the class loaded by the ClassLoader on the right, and the mechanism for loading the class to determine whether it has loaded the class or not is asking for a superior.
These three class loaders, respectively, correspond to the compiler to look for the priority level of the class file and the different paths:
- Bootclassloader It is written in C + +, the class is loaded from the%jre%/lib directory, or the runtime uses the-xbootclasspath specified directory to load. Is the compiler's first place to look for class.
- Extclassloader classes are loaded from the%jre%/lib/ext directory, or the runtime uses-djava.ext.dirs to create a directory to load. It's the compiler's second priority for finding class.
- Systemclassloader is what we often say appclassloader, it corresponds to the current path, so it is the compiler default find class place.
The Class.forName () used in the project will be queried from Bootstrapclassloader, which is the most resource consuming.
Source code author here uses thread class loader, corresponding to Systemclassloader, the efficiency is undoubtedly the highest.