- red is warning, blue is suggestion
-
1.use static factory instead of contractors
-
static factory VS contractor
- has name , easy to understand
- do not need create a instance when call it
-
can return any subclass instance
- for example: EnumSet
- client do not care which subclass will be used
- support future subclass
- reduce the verbosity of creating parameterized type instances.
- can not subclassed one class without public or protect contractor
-
no differce with other static method , hard to find it
-
common static factory name
- valueOf
- of
- newInstance
- getType
- newType
-
2.use builder when contractor has many parameters
-
old methods
-
many contractor method
- hard to read and control
-
use setter method of javabean
- a JavaBean may be in an inconsistent state partway through its construction
-
builder
- can check parameter
-
can own many varargs
- jdk5
- only use it when has many parameter , else impact performance
-
3.private contractor or enum to implate Singleton
-
before jdk5
- static final instance and private constructor
- static factory and private constructor
-
jdk5
-
public enum class{ INSTANCE ..}
- easy serializable
- avoid instance
-
4.enforce noninstantiability with private constructor
- can not be subclassed
-
5.avoid create unnessary instance
- as soon as reuse instance
- use static factory better than constructor, do not create new instance
- reuse unmutabled object
- to do link to defensive copying
-
6.Eliminate obsolete object references
-
memory leak
- Disk Paging
- OutofMemoryError
-
source
- reference expired
- cache not cleared
- register a rollback and not cancel register
-
solution
- set null
- heap
-
7.avoid use finalizer
- can not sure it will be excuted
- impact performance
-
call terminate method in finally
- do not forget call super.finalized()
- 分支主题 8