Summary of design patterns involved in JDK

All design patterns have found an implementation in JDK (not all are listed, because there are too many reasons, and the purpose of this sorting is to sort out and memorize design patterns through examples in JDK, so only one implementation is written). Introduce classes, corresponding methods and functions in turn.

Create mode

< H4 id = "1 Abstract Factory" > 1 Abstract factory

java. Lang. StringBuilder, which is a final class public StringBuilder append (string STR) method, which overrides the parent class. Class function: used for a character sequence that cannot be changed. Method function: according to the existing character sequence and additional characters, copy the method system through the system Arraycopy generates a new character sequence.

java. text. NumberFormat abstract class public final static numberformat getinstance() method class function: an abstract base class for digital format. Method function: returns a numberformat of "a general number format in the current default scene". Obviously, it belongs to the use of factory mode.

java. Lang. object protected native object clone() method class function: the parent class method function of all classes: return a shallow copy object according to the existing instance.

java. Lang. runtime class public static runtime getruntime() class function: each running Java application will have a unique instance of runtime class, which enables the application to be affected by the running environment during running. Method function: returns a runtime object associated with the current Java application. Implementation method: private static runtime currentruntime = new runtime();

The < H4 id = "8 combination mode" > 8 is not found at present Combination mode

Reader abstract class and writer abstract class have the same constructor function. Constructor function class function: reader abstract class is used to read a character set stream; The writer abstract class is used to write a character set stream. Method function: it is the function of constructor

javax. faces. context. Externalcontext. ServletContext, httpsession, HttpServletRequest, httpservletresponse, etc. are used internally.

java. Lang. integer (the same is true for other basic type wrapper classes (except float, double) and BigDecimal) The wrapper of valueof () method byte, short, int, long, Boolean and char has cached the object reference of the specified range when the class is loaded into the JVM, because the value setting uses static blocks or constants. The range of char is 0 ~ 127; Boolean values are true and false; Other default ranges are - 127 ~ 128. The upper limit 127 of int can be adjusted, which requires adjusting the parameters of the JVM. At the same time, the string class also makes use of the meta mode, because each string is immutable.

java. lang.reflect. Principle of proxy class: proxy provides a static method to create an instance of dynamic proxy class and proxy class. It is the parent of all dynamic proxy classes created in this way. Usage: invocationhandler = new myinvocationhandler (...);

java. Lang. runnable all implementation class functions of the runable interface: classes that implement the runable interface can be executed by threads. If the command mode is embodied: encapsulate the request as an object through the class implementing the runable interface, queue the request or record the request log, and support revocable operations. The most important thing that allows the party receiving the request to decide whether to veto the request is that the command mode decouples the object requesting an operation from the object how to perform an operation. This is the embodiment of the task class that implements the runable interface implemented by the excutor framework.

java. text. Format class function: this abstract class is used to format some format sensitive information, such as date, information, number, etc. Implementation method: this class defines some methods to convert format sensitive information into string.

Not to mention this, many collections have been traversed by iterators< H4 id = "17 intermediary mode" > 17 Intermediary model

java. io. Serializable interface function: only classes that implement the serializable interface can be serialized. There are no methods in this interface, but the classes that implement this interface can be serialized. If a class wants to serialize, in addition to implementing this interface, it also needs to write its own readobject() and writeobject() methods for reading and outputting streams.

java. util. EventListener interface class function: all event listening interfaces must extend this interface.

javax. faces. webapp. The execution of the facesservlet instance method depends on the instance state.

java. util. Comparator function interface compare() method comparator is used to write a comparator first, and then when sorting arrays or collections, pass the written comparator as a parameter into the sorting method to sort arrays or collections according to the rules of the comparator. How the comparator method reflects the policy pattern: the policy pattern refers to the behavior of a class / interface a, and the execution of the method is realized by calling another different class / interface B. The calling method is to take B as the incoming parameter of A. This is completely consistent with the principle of comparator.

java. util. How all non abstract methods of abstractlist embody the template method pattern: the template method pattern definition, that is, in an abstract class, the behavior method always has a default behavior, and the subclass can be used directly or overridden. ArrayList inherits abstractlist. Methods in abstract are directly used when methods without overrides are used.

java. nio. file. Filevisitor interface class function: an interface for accessing files. The implementation class of this interface is through files The walkfiletree method accesses each file in the file tree. Public static path walkfiletree (path start, set options, int maxdepth, filevisitor < super Path > visitor), which calls back the visitor method. Method implementation: the visitor has an implementation method for each visitor. Each visitor has a general method. The input parameter is the visitor. This method is used to call the visitor's method. Reference: nofollow "> https://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>