The design principles and ideas of the Invariant framework (Invariant) framework

The design principles and ideas of the Invariant framework (Invariant) framework Invariant is a very important concept in programming, which refers to maintaining unchanged attributes or states in the life cycle of the object.In the design of the Java class library, the use of constant frameworks can make the code more robust, maintenance and easy to understand. The principles of unchanging frameworks are as follows: 1. Make the class immutable: A clarity refers to a class that cannot be modified once it is created.In order to achieve unavailable categories, we need to declare all fields as private and do not provide public methods to modify them.Moreover, the non -mutinal classes should be final to prevent subclasses from modifying its behavior by inheriting. The following is a sample code that shows how to create an immutable Person class: public final class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } 2. Use private attributes: In order to protect the invariability, the field of the class should be declared as private and accessed them through the accessor method.This can prevent external code modify the state of the object and control the access of the field. 3. Avoid exposure to variable objects: If the non -variable class contains the reference to variable objects, then you should handle these references cautiously to prevent external code from modifying them.You can use a copy constructor or deep copy to create unavailable copies. Below is a sample code that shows how to deal with variable objects: public final class ImmutableClass { private final List<Integer> numbers; public ImmutableClass(List<Integer> numbers) { this.numbers = new ArrayList<>(numbers); } public List<Integer> getNumbers() { return new ArrayList<>(numbers); } } 4. Reappearing: In order to ensure that unsatisfactory is not variants, you can add some inspections to the constructing function of unchanged categories.For example, you can check whether the parameters are legal, whether it is null, or whether it meets certain conditions.If the inspection is not approved, it can be thrown out to prevent the creation of the unsatisfactory object. The following is a sample code that shows how to conduct non -degeneration inspections in the constructor: public final class ImmutableClass { private final int number; public ImmutableClass(int number) { if (number < 0) { throw new IllegalArgumentException("Number must be positive"); } this.number = number; } public int getNumber() { return number; } } The idea of using the constant framework is as follows: 1. First of all, determine which categories need to be changed, that is, which categories should not be modified after creation. 2. Determine the non -variability constraints, including which fields should be immutable, which fields should be private, which field reference variable objects, etc. 3. Implement unavailable categories, design and encoded in accordance with the above principles. 4. Invasive inspections are performed in the constructor of the class to ensure that the non -degeneration conditions are met. 5. Provide access to access to the state of the object to avoid references to variable objects. 6. Provide a cloning method or a copy constructor to return a copy of the uncharacteristic object. By following the principle of invariance and the use of unchanged frameworks, the reliability, maintenance and readability of the code can be improved.It is easier to understand that unavailable classes, because their state will not change, and it will not cause accidents due to the modification of external code.In addition, invariability can also bring thread security, because multiple threads can share unchanging objects without worrying about competitive conditions.Therefore, the design and use of the unchanged framework in the Java library is very valuable. Reference materials: - Effective Java by Joshua Bloch - Java Concurrency in Practice by Brian Goetz