Notes of “Effective Java” (Chapter 7-11)

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
  • Assert

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.

Read the rest of this entry »

Advertisements

Notes of “Effective Java” (Chapter 1-6)

Item 1: Consider static factory methods instead of constructors

advantage:

  1. Customized meaningful names better than constructors.
  2. Associated with class instead of creating new objects.
  3. 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.

Read the rest of this entry »