Design concept
Because no synchronization is required, it handles concurrency control by avoiding the problem rather than solving the problem.
To build the invariant pattern:
1, this class itself is final, then this class will not be able to have sub-class, so there is no way to have quilt class modification problem.
2. Remove setter methods and other methods that can modify their own properties.
The difference between invariant and read-only
the "invariant" (immutable) is different from Read only. When a variable is "read-only", the value of the variable cannot be changed directly, but it can change when other variables change. For example, a person's date of birth is a "constant" attribute, and a person's age is a "read only" attribute, not a "invariant" attribute. As time changes, the age of one person changes, and the person's date of birth does not change. This is the difference between "unchanging" and "read-only".
Invariant mode is applied in the JDK
string+ all the original data wrapper classes: They are widely used in the actual development, in the multi-threaded environment, save a lot of lock synchronization caused by the overhead. , improved performance
Invariant Mode overview