Introduction to the framework of the principle of invariability in the Java library
Introduction to the framework of the principle of invariability in the Java library
Immutability Principle is an important principle in object -oriented programming. It means that once the object is created, its state cannot be modified.Unity refers to the state of the object that cannot be changed, not just the value in the object cannot be modified.The principle of non -degeneration helps to improve the readability, reliability and concurrency of the code, and reduce the probability of errors.
In the Java class library, there are some frameworks and tools that can help achieve the principle of invariability. The following are the introduction of several commonly used frameworks:
1. Java built-in Immutable Classes: Java provides some string, packaging classes (such as Integer, Double, etc.), and collection classes (such as list, set, etc.).After the creation, these classes are unchanged, and any modification operation will return a new object.
Example code:
String name = "Alice";
String modifiedName = name.touppercase (); // generate a new string object, instead of modifying the original string object
2. GUAVA's Immutable Collections: Google's GUAVA library provides a set of non -changing collection classes, such as ImmutableList, ImmutableSet, and ImmutableMap.After the creation, these collection classes are not allowed to modify their content, but they can use the original part or all elements to create a new collection.
Example code:
ImmutableList<String> colors = ImmutableList.of("Red", "Green", "Blue");
ImmutableList<String> modifiedColors = ImmutableList.<String>builder()
.addAll(colors)
.add("Yellow")
.build (); // Create a new collection with B use of builder, which contains all the elements and new elements of the original collection
3. LOMBOK's @Value Note: LOMBOK is a Java library that is used to reduce the writing of model code by annotating.Among them, @Value annotations can help create unavailable classes, and automatically generate methods such as Getter, Equals, HashCode, and Tostring.
Example code:
import lombok.Value;
@Value
public class Person {
private final String name;
private final int age;
}
Person person = new Person("Bob", 25);
String name = Person.getName (); // The automatic Getter method generated automatically generates the Getter method
In addition to the above -mentioned frameworks, there are other third -party libraries and frameworks, such as the Immutable class of Apache Commons, ImmutableList, and Mapstructs of Eclipse Collections, which also provide the function of implementing the principle of invariance.
In summary, implementing the principle of invariability can enhance the readability and reliability of the Java class library, and reduce the problems in concurrent programming.The unrealistic classes in the Java class library, the IMMUTable Collections of the GUAVA, and the @Value annotation of LOMBOK can help developers to easily create and use unsatisfactory objects.