The state of an object does not change after the object is created. This is the so-called constant mode.
--------------------------------------------------------------------------------
Unchanged structure
The constant mode enhances the robustness of an object ). The constant mode allows multiple objects to share an object, reducing the synchronization overhead for concurrent access to the object. If you need to modify the state of an unchanged object, you need to create a new object of the same type and store the new State in the new object.
The unchanged mode only involves one class. After the internal status of a class is created, the class will not change throughout the lifecycle. This method of using the unchanged class is called the unchanged mode. There are two ways to change the mode: one is the weak mode and the other is the strong mode.
Weak Mode
The status of an instance of a class cannot be changed, but the instance of the subclass of this class may change. This class conforms to the definition of the weak unchanged mode. To implement the weak and unchanged mode, a class must meet the following conditions:
First, the object to be considered does not have any way to modify the object State. In this way, when the object constructor initializes the object state, the object state will not change.
2. All attributes should be private. Do not declare any public attributes to prevent the client object from directly modifying any internal status.
Third, if the other objects referenced by this object are variable objects, you must try to restrict access to these variable objects to prevent them from being modified. How is it possible to initialize these referenced objects within the unchanged object, instead of initializing them on the client, and then passing them into the unchanged object. If a mutable object must be initialized on the client and then transmitted to the unchanged object, you should consider copying this mutable object during initialization, instead of using the original copy.
The disadvantage of the weak unchanged mode is:
First, the sub-object of a weak unchanged object can be a mutable object. In other words, the sub-object of a weak unchanged object may be changeable.
Second, this variable sub-object may be able to modify the state of the parent object, which may allow the outside world to modify the state of the parent object.
Strongly unchanged Mode
The instance of a class will not change, and the instance of its subclass also has an unchangeable state. This class conforms to the strongly unchanged mode. To implement the strongly unchanged mode, a class must first meet all the conditions required by the weak unchanged mode and meet one of the following conditions:
First, all methods of the class to be considered should be final, so that the subclass of this class cannot replace this class.
Second, the class itself is final, so this class may not have child classes, and thus it is impossible to have the problem of modifying the quilt class.
What is the difference between "unchanged" and "read-only "?
Immutable is different from Read-Only. When a variable is read-only, the value of the variable cannot be changed directly, but it can be changed when other variables change.
For example, a person's birth year, month, and day are "unchanged" attributes, while a person's age is "read-only" attributes, not "unchanged" attributes. As time changes, the age of a person changes, and the birth year, month, and day of a person does not change. This is the difference between "unchanged" and "read-only.
Applications of the unchanged mode in JAVA
The most famous application of the unchanged mode in JAVA is the java. lang. String class. The String class is a strongly unchanged type. When the following statement is displayed:
[Java]
String a = "test ";
String B = "test ";
String c = "test ";
String a = "test ";
String B = "test ";
String c = "test ";
Java Virtual Machine will actually only create such a String instance, and these three String objects are sharing this value.
Advantages and disadvantages of the constant mode
The constant mode has obvious advantages:
(1) Because you cannot modify the state of a constant object, you can avoid unnecessary program errors. In other words, a constant object is easier to maintain than a variable object.
(2) because no thread can modify the internal state of the unchanged object, a unchanged object is automatically thread-safe, which saves the overhead of synchronization processing. An unchanged object can be freely shared by different clients.
Disadvantages of the constant mode:
The only drawback of the constant mode is that once you need to modify the state of a constant object, you have to create a new similar object. In an environment that requires frequent modification of unchanged objects, a large number of unchanged objects are created as intermediate results and collected by the JAVA garbage collector. This is a waste of resources.
When designing any class, you should carefully consider whether its status needs to change. Unless it is necessary to change its status, it should be designed as a constant class.
Author: m13666425773