Item 37: Use marker interfaces to define types
Marker interface contains no method declarations, which indicates some property or features. The typical marker interface is Serialize interface.
Item 38: Check parameters for validity
Check and restrict the parameters of functions.
- Throw Exception
Item 39: Make defensive copies when needed
In general, if a class has mutable components that it gets from or returns to its clients, the class must defensively copy these components.
Defensive copy usually implemented by:
- clone() method
- copy constructor
- static factory
This is not mandatory. If the class trusts the clients, then defensive copy could be replaced by documentation explanation to save the cost.
Item 1: Consider static factory methods instead of constructors
- Customized meaningful names better than constructors.
- Associated with class instead of creating new objects.
- Able to return subtypes.
For the 2nd advantage, static factory methods form the basis of Service provider Framework including service interface, provider interface, provider registration API and service access API.
Item 2: Consider a builder when faced with many constructor parameters
Compare with telescoping constructors and JavaBean Style.
Builder is set as a static inner class. Builder makes the object initialized immutable.