In Java, are you still unchanged new corresponding objects for instantiating the class?
With more projects in hand, the amount of code writing will naturally increase, and gradually you will feel the design pattern.
How to make the written class instantiation actions, high cohesion, low coupling, and have certain expansion capabilities?
This article tries to start with a few vivid codes to present different Java instantiation classes to everyone.
The following code is taken from the com.google.zxing source code 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); }The BarcodeFormat is like 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() { }} The function provided by the source code is to output information into a bit matrix through several different types of barcode Wirters, and then output it to the picture to form various types of barcodes that can be seen everywhere.
BitMatrix bitMatrix = new MultiFormatWriter().encode(_text, BarcodeFormat.QR_CODE, qrcodeWidth, qrcodeHeight, hints);
MatrixToImageWriter.writeToFile(bitMatrix, qrcodeFormat, QrcodeFile);
The source code author uses the new feature enum enum class introduced in JDK 1.5 here and wrote the BarcodeFormat class, which defines the properties of different types of barcodes.
Call MultiFormatWriter.encode() to instantiate the specific class according to the sequence number of the 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 implement two encode() methods of abstract interface Writer.
public interface Writer { BitMatrix encode(String var1, BarcodeFormat var2, int var3, int var4) throws WriterException; BitMatrix encode(String var1, BarcodeFormat var2, int var3, int var4, Map<EncodeHintType, ?> var5) throws WriterException;}The specific barcode Wirter class performs different logics based on different types of barcode rules.
Users do not need to pay too much attention to the internal implementation. They need to generate what kind of barcode they need to choose and use the appropriate barcode type. The above examples are implemented in the QR code.
Let’s look at a piece of method code for the dynamic instantiation class of the classic MVC framework Webwork:
private static Configuration getDefaultConfiguration () { if (defaultImpl == null) { defaultImpl = new DefaultConfiguration(); 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) { } } }The source code is taken from webwork-core. Many spectators may not have heard of Webwork, but Struts should be well-known. Struts2 core is rewritten from Webwork.
The function provided by the above source code is to instantiate the configuration file reading class defined by the user, and the definition is in the configuration file.
The source code author uses Thread.currentThread().getContextClassLoader().loadClass(className) class loader in thread to dynamically instantiate custom configuration file reading classes, which is the most efficient way.
Delegate chain of class loader: SystemClassloader -> ExtensionClassloader -> BootstrapClassloader
The ClassLoader on the left side of the delegation chain can naturally use the class loaded by the ClassLoader on the right. The class loading mechanism is to determine whether it is loading the class, and it is not asking the superior.
These three class loaders correspond to the compiler's priority and different paths to find the class file:
Class.forname() used in daily projects will start asking from BootstrapClassloader, which is the most resource-consuming.
The source code author uses a thread class loader here, corresponding to SystemClassloader, which is undoubtedly the highest efficiency.