The best practice and experience sharing of the Invariant framework
The best practice and experience sharing of the Invariant framework
Invariant refers to the characteristics that can no longer be modified after a object was created.In Java, using unchanged objects can provide safer and reliable code design.In order to achieve unchanged features, we can use the constant framework.This article will introduce the best practice and experience of constant framework, as well as some Java code examples.
1. Use the final keyword to ensure the immutability of the attribute
In the constant framework, all attributes should be declared as final so that their values cannot be modified.This can be implemented by adding the final modifier to the class.For example:
public final class ImmutableClass {
private final int number;
private final String text;
public ImmutableClass(int number, String text) {
this.number = number;
this.text = text;
}
public int getNumber() {
return number;
}
public String getText() {
return text;
}
}
In the above examples, both Number and Text attributes are declared as final, which means that they cannot be modified after the object is created.
2. Do not provide the setter method
In order to maintain the non -degeneration of the object, any method to modify the attribute value, such as the setter method.Set the attribute to private and provide the Getter method that is only read, which can ensure that the attribute is only read.For example:
public final class ImmutableClass {
private final int number;
private final String text;
public ImmutableClass(int number, String text) {
this.number = number;
this.text = text;
}
public int getNumber() {
return number;
}
public String getText() {
return text;
}
}
3. Package of protective attributes
In order to ensure the packaging of attributes, you should try to avoid using variable objects as attributes in non -changing objects.If the object contains the attributes of variable objects, it should be protected to copy these attributes and return the copying object.This can prevent external code from modifying the internal attributes.For example:
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);
}
}
In the above example, by protecting a protective copy in the constructor, the Numbers list that can be passed in can not be modified by the external code.
4. Avoid modifying attributes
In the constant framework, any attributes should be avoided.On the contrary, new constant objects should be returned.This can be implemented by returning a new object, rather than modifying the attributes of the current object.For example:
public final class ImmutableClass {
private final int number;
public ImmutableClass(int number) {
this.number = number;
}
public ImmutableClass add(int value) {
return new ImmutableClass(number + value);
}
public int getNumber() {
return number;
}
}
In the above example, the ADD method returns a new ImmutableClass object instead of modifying the attributes of the current object.
5. thread security
Because the status of the unchanged object is immutable, they are safe threads.Therefore, the use of unchanged objects in a multi -threaded environment is a good practice.
Summarize:
Using the unchanged framework can provide a safer and reliable code design to avoid accidental modifications.When achieving unchanging objects, the final keyword should be used to ensure the unmodified attribute, avoid providing the setter method, protecting the package of attributes, and returning a new constant object instead of modifying the attributes of the current object.It is safe to use unchanged objects in a multi -threaded environment.
I hope the best practice and experience provided in this article will help you use the constant framework in the Java class library!