Immutable objects
Private constructor
Implementing toString
Data access objects
Avoid clone
Abstract Factory
getLogManager
compile
unmodifiableCollection
,
Collections.synchronizeCollection
, and so onnow
getInstance
and valueOf
.
These names are not mandatory - choose whatever makes sense for each case.
Example
public final class ComplexNumber { /** * Static factory method returns an object of this class. */ public static ComplexNumber valueOf(float real, float imaginary) { return new ComplexNumber(real, imaginary); } /** * Caller cannot see this private constructor. * * The only way to build a ComplexNumber is by calling the static * factory method. */ private ComplexNumber(float real, float imaginary) { this.real = real; this.imaginary = imaginary; } private float real; private float imaginary; //..elided }