[Conclusion] Objective java experiences, creating and destroying objects, and using objective java
Reprinted please indicate the source: http://blog.csdn.NET/supera_li/article/details/44940277
I have some summative thoughts on objective Java. The length may not follow the directory, because you can directly read which chapter you like to read first. However, it is certain that each chapter will have a summary. You are welcome to make bricks and supplements.
Valid java Series
1. Using objective java experiences to create and destroy objects
2. Positive java experience, generic
3. Positive java experiences, classes and interfaces
4. Positive java experiences, general methods
5. Positive java experience, enumeration, annotation, method, general design, exception
6. Positive java experience and concurrent programming
1.Consider replacing the constructor with the static factory Method. Advantage: there is a name, so you do not have to create an object each time. It returns any child type object and concise code. Disadvantage: This class cannot be subverted (composite is greater than inheritance, which is also an advantage). It is not convenient for the doc tool to output documents. Generally, the naming rules are as follows:
ValueOf Conversion Type
GetInstance
NewInstance
GetType: Get the type of the returned object
Insert newType into the type of the returned object to distinguish factory methods in different classes.
2. When encountering multiple Constructors (more than four), consider using the builder.The code of the overlapped constructor is lengthy and cannot be effectively maintained in the later stage. The constructor In the javaBean mode is insecure under multiple threads and does not achieve class immutations because the setter method is exposed. Builder mode constructor is a good choice.
Class {
// Attributes
Static class Bulid {
// Attributes
Bulid () {// accept initialization parameters}
Bulid a () {Return bulid; // Add parameters Return Bulid generation call chain}
..
A bulid () {return A (this);} // The constructor object initializes object A and returns
}
A (Bulid B) {// pass the parameters in B to the parameter field}
}
3. Use a private constructor or enumeration type to enhance the Singleton attribute.There are two solutions for private constructor: Exporting public static member instances and providing static factory return private static member instances. But it is not safe. Using AccessibleObject, the reflection technology can call a private constructor to generate the second object. The solution is to throw an exception when the second instance is generated. Enable Singleton serialization, (implementsSerializble) + (instance domain transient) + (readResolve method returns the instance ).The enumeration type of a single element is the best solution for Singleton.
4. Enhance the non-instance capability through the private constructor. The displayed constructor is better than the default constructor, especially the private constructor, And the AssertionError is thrown in the private constructor. Because of the use of this technology, this class cannot be divided into categories.
5. Avoid creating unnecessary objects.The following fields are considered: string class, static {} field, view (set in Map), automatic packing and unpacking, and object pool.
6.Removes expired object references.After Stack. pop, memory leaks and disk exchanges are caused by unempty unused elements. In severe cases, OOM exceptions are caused. Another source of Memory leakage is cache problems. If the key is not always referenced, WeakHashMap automatically clears the expired key. If some keys may not be used as time increases, use LinkedHashMap. The removeEldestEntry method implements this scheme. The third source is the listener and other Callbacks. It adopts the WeakHashMap solution, which maintains weak references. Heap Profiler, a Heap profiling tool for Memory leakage.
7.Avoid using the termination method.The Finalizer method JVM does not necessarily execute, even if System. gc is called. System. runFinalization. Unless the System. runFinalizersOnExit and Runtime. runFinalizersOnExit methods are used (ThreadStop, not recommended ). Try to use the try {} finally {} structure to display the call termination method to ensure timely termination. If the subclass overwrites the termination method of the superclass, but forgets to manually call the termination method super. finalize of the superclass, the termination method of the superclass will never be called.
What if I relate the content of this chapter to the actual encoding? I am also thinking that some suggestions may not be accurate because I have little development experience in class libraries and have not read all the source code of jdk.
1. When creating a class, you should think about what the class will do in the future? What type is it? Are there many construction parameters? Applicable experience: 1, 2, 4
2. When writing a code segment, should you think of whether the object can be reused? If it is a collection class, do you think that the input object will not be used in the future? If you think that this Code or method and the class involved will consume a large amount of memory as it runs globally? We can try to solve this problem by using three summaries: 5, 6, and 7.
After all, there are some methodologies above. In practice, you need to have experience to determine how to do the best (maybe the best at the moment, or the best effect in the future ), these are generated in different scenarios and may not be effectively solved.