You often read about irreversible objects that the last areas should be irreversible in Java. Is it really the case or is it not clearly enough that there is no public variability and do not mutate the state?
For example, if you have an immutable object created by the builder pattern, then you can do this by assigning different fields to the builder because it creates it, or builder The fields are kept, and ultimately, by passing the values to its (private) constructor, returns the immutable thing.
The obvious benefit of preventing errors in implementation (such as allowing the code to retain the builder's reference and keeping the object multiple times, muting an existing object), but the builder's data It appears as DRYer is stored in the object as it is created.
So the question is: Believing the builder, the object does not snatch away quickly and prevents itself from changing the builder (by setting the object's references to zero), anything actually received (Such as improved thread safe AT) if the object fields are finalized in the "irreversible" of the object?
Yes, you get "thread protection" from the final
areas The value assigned to a final
field during the construction is guaranteed to be visible to all threads. Another option for thread protection is to declare the unstable
fields, but then you read the upper part with each read; And who sees your class and wonders why the area of this "irreversible" class has been marked as "volatile".
Marking the field final
is most technically correct, and clearly clarifies your intentions. Unfortunately, this builder pattern makes it very complicated I think It is possible to create an annotation processor to synthesize the builder for a temporary classroom, as the project does with the Lombok servers and the gestures will require real work IDE support so that you Can code against Dron who do not actually exist.
Comments
Post a Comment