Invariant Mode overview

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.