* 1, consider the static Factory method (staticfactorymethods) instead of the constructor? ! Advantage? Insufficient?
Service Provider Architecture:
1. Service Interface (Interface)-Provider implementation;
2, Provider registration API (Provider Registration API)--the system is used to register the implementation, let the client access to them;
3. Services Access API (service access API)--the client is used to obtain an instance of the service; [It is a "flexible static factory" that forms the basis of the service provider architecture!) ]
* 4, Service Provider Interface (Services Provider Interface)--These providers are responsible for creating instances of their service implementations
[If there is no service Provider Interface, the implementation is registered according to the class name and instantiated by reflection.
For JDBC,
Connection is its service interface,
Drivermanager.registerdriver is to provide the registration API,
Drivermanager.get connection is the service Access API,
Driver is the service provider interface]
2. when encountering multiple constructor parameters, consider using the constructor
* 3, with private constructor or enum type hardening Singleton Property
4, the ability to strengthen the non-instantiation through the private constructor
5. avoid creating unnecessary objects
*6, elimination of expired object references
//Can you spot the "Memory leak"? Public classStack {Privateobject[] elements; Private intSize = 0; Private Static Final intDefault_initial_capacity = 16; PublicStack () {Elements=NewObject[default_initial_capacity]; } Public voidpush (Object e) {ensurecapacity (); Elements[size++] =e; } PublicObject Pop () {if(Size = = 0) Throw Newemptystackexception (); returnelements[--size]; } /*** Ensure space for at least one more element, roughly * doubling the capacity each time the array needs to GR ow. */ Private voidensurecapacity () {if(Elements.length = =size) Elements= arrays.copyof (Elements, 2*size+1); }}
BUG: "Memory Leak", as the garbage collector activity increases, or due to the increasing memory consumption, the performance of the program is gradually reduced. Extreme situations,
can cause disk Paging, or even cause program error (OUTOFMEMORYERROR)--Rare!
Eg: If a stack grows first and then shrinks, the objects that are popped from the stack will not be garbage collected, even if the program using the stack no longer references these objects, they
Will not be recycled. Because: the stack internally maintains an outdated reference to these objects (obsolete reference)-a reference that will never be lifted again. In this example, elements
Any references outside the active part (active portion) of the array are out of date. The active part refers to: elements those elements less than size.
Fix BUG: These references need to be emptied once the object reference expires. That is, modify the Pop method, as follows;
Public Object Pop () { if (size = =0 )thrownew emptystackexception ( ); = elements[--size]; NULL // eliminate obsolete reference. return result; }
Benefits:
1, to solve the problem of long-term memory consumption, reduce performance;
2. If they are subsequently incorrectly dereferenced, the program throws a NullPointerException exception instead of silently continuing!
/**
* 1, as long as the management of their own memory, programmers should be wary of memory leaks;
*
* 2, cache;--put the object reference into the cache, it is easy to be forgotten;
[FIX: If there is a reference to the key of an item outside the cache, it makes sense, then you can use Weakhashmap to represent the cache!]
"The life cycle of cached items makes sense"--not easy to determine. The Linkedhashmap class uses its Removeeldestentry method!
More complex caches are required to use java.lang.ref! directly ]
*
* 3, Listener & other callbacks
[Eg: if you implement an API, the client registers the callback in this API, but does not explicitly unregister it, so unless you take some action,
They will gather. The best way to ensure that callbacks are immediately treated as garbage collection is to save only their weak references (weak reference), for example, to only
Save as a key in Weakhashmap. ]
*
**/
7, avoid the use of the End method ? (ie finalizer)
"Don't treat the finalization method as a counterpart to the C + + destructor!" (constructor) "
C + +: The destructor (destructors) reclaims the resources used by an object and is a necessary counterpart to the constructor;--You can also reclaim other non-memory resources
Java: When an object becomes unreachable, the garbage collection mechanism reclaims the storage space associated with the object;--using try-finally for similar work
----------------------------
"Effective Java "
--Second Edition (James Gosling)
----------------------------
[Java] I. Creation & destruction of objects