Item 15 The variability of the minimized class
Effective Java
How do I make a class immutable?
- Does not provide a way to change the state of an object. (mutators)
- Ensure that the class is not inherited, that is, the class is qualified with final.
- Make all fields (field) final.
- Make all the domains private.
- Ensure all mutually exclusive access to the mutable component (ensure exclusive access to all mutable components)
Example:
Public Final classcomplex{//final class Private Final DoubleRe//private final Field Private Final DoubleIm Public Complex(DoubleReDoubleIM) { This.Re= RE; This.im= IM; } Public Double Realpart(){returnRe;} Public Double Imaginarypart(){returnim;} PublicComplexAdd(Complex c) {return New Complex(Re+c.Re, Im+c.im);//return New Object instead of modify itself}//other Arithmetic method ommit ...}
Why use immutable objects (immutable objects)?
- Immutable objects are simple. There is no need to consider whether an object will behave strangely in change. Immutable objects are always valid as long as there is a limit in the construction method.
-
Immutable objects are inherently thread-safe and do not require synchronization (synchronization). Immutable objects are encouraged to share without worrying about security. For example, the following:
publicstaticfinalnewComplex(0,0);
- The interior of an immutable object is also encouraged to be shared.
-
Immutable objects are excellent as artifacts of other complex objects (building blocks). Its invariable nature reduces the difficulty of designing complex objects.
Disadvantages of immutable objects and optimization
- The only disadvantage of immutable objects is that each operation requires the creation of new objects that cause performance problems in multi-step operations.
- Optimization:
- Use the variable "adjoint class" (mutable "companion class") in multi-step operations. For example, the string class and the string buffer class.
- predict which multi-step operations are often used, and provide an official method for optimizing these operations.
Best practices
- Provides public static factory methods (static Factory method) and makes the constructor private.
- You can detect the validity of input data in advance, and avoid creating objects directly if they are not legal.
- You can cache some of the commonly used objects to optimize performance.
- You can add some useful features.
- The conditions that make immutable classes can be weakened to any method that does not result in changes in the external representation of the object . This allows us to create some internal mutable components for caching, which optimizes performance.
Summarize
- When designing a class, consider first whether you can make it immutable.
- If a class cannot be made completely immutable, its variability is limited to a minimum.
- Do not provide any initialization methods outside of the static factory method and the constructor.
"Effective Java" reading notes-minimizing the variability of classes